Hello everybody,
I am a newbie to python so I hope I am at the right place to expose my
problem..... :-[
I am working on linux mandrake 10.1 with python :
python -V
Python 2.3.4
I am trying o run the example which stay in the documentation in paragraph
http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding
I download the code example from
I call the file "TestOfficiel.c " and I compile it with :
gcc -g -I/usr/include/python2.3/ TestOfficiel.c -o TestOfficiel
-lpython2.3 -ldl
all is OK (or seems to be...).
as stated in the documentation I creat a file "TestPythonFoo. py" which
contain
"
def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
"
I launch
../TestOfficiel ./TestPythonFoo.p y multiply 3 2
and as a result :
ValueError: Empty module name
Failed to load "./TestPythonFoo.p y"
if I try an absolute path to the python file :
../TestOfficiel `pwd`/TestPythonFoo.p y multiply 3 2
I obtain :
ImportError: No module named
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y
Failed to load
"/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y"
Of course the file exist :
[montmory@esoppe 1 swigCallPython]$ ll
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y
-rwxrwx--x 1 montmory esoppe 126 sep 29 14:04
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y*
I found lot of post about "ValueError : Empty module name" but no clear
solution (clear for me...).
What's wrong ?
my python version?
Additionnal informations :
gcc version 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)
Thanks for your help,
best regards,
Alain
#include <Python.h>
int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize() ;
pName = PyString_FromSt ring(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import (pName);
Py_DECREF(pName );
if (pModule != NULL) {
pDict = PyModule_GetDic t(pModule);
/* pDict is a borrowed reference */
pFunc = PyDict_GetItemS tring(pDict, argv[2]);
/* pFun: Borrowed reference */
if (pFunc && PyCallable_Chec k(pFunc)) {
pArgs = PyTuple_New(arg c - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong( atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs );
Py_DECREF(pModu le);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem (pArgs, i, pValue);
}
pValue = PyObject_CallOb ject(pFunc, pArgs);
Py_DECREF(pArgs );
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pV alue));
Py_DECREF(pValu e);
}
else {
Py_DECREF(pModu le);
PyErr_Print();
fprintf(stderr, "Call failed\n");
return 1;
}
/* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
}
else {
if (PyErr_Occurred ())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_DECREF(pModu le);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
I am a newbie to python so I hope I am at the right place to expose my
problem..... :-[
I am working on linux mandrake 10.1 with python :
python -V
Python 2.3.4
I am trying o run the example which stay in the documentation in paragraph
http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding
I download the code example from
I call the file "TestOfficiel.c " and I compile it with :
gcc -g -I/usr/include/python2.3/ TestOfficiel.c -o TestOfficiel
-lpython2.3 -ldl
all is OK (or seems to be...).
as stated in the documentation I creat a file "TestPythonFoo. py" which
contain
"
def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
"
I launch
../TestOfficiel ./TestPythonFoo.p y multiply 3 2
and as a result :
ValueError: Empty module name
Failed to load "./TestPythonFoo.p y"
if I try an absolute path to the python file :
../TestOfficiel `pwd`/TestPythonFoo.p y multiply 3 2
I obtain :
ImportError: No module named
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y
Failed to load
"/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y"
Of course the file exist :
[montmory@esoppe 1 swigCallPython]$ ll
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y
-rwxrwx--x 1 montmory esoppe 126 sep 29 14:04
/space/ESOPPE_PROJET/Outils/SwigPython/swigCallPython/TestPythonFoo.p y*
I found lot of post about "ValueError : Empty module name" but no clear
solution (clear for me...).
What's wrong ?
my python version?
Additionnal informations :
gcc version 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)
Thanks for your help,
best regards,
Alain
#include <Python.h>
int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr, "Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize() ;
pName = PyString_FromSt ring(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import (pName);
Py_DECREF(pName );
if (pModule != NULL) {
pDict = PyModule_GetDic t(pModule);
/* pDict is a borrowed reference */
pFunc = PyDict_GetItemS tring(pDict, argv[2]);
/* pFun: Borrowed reference */
if (pFunc && PyCallable_Chec k(pFunc)) {
pArgs = PyTuple_New(arg c - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong( atoi(argv[i + 3]));
if (!pValue) {
Py_DECREF(pArgs );
Py_DECREF(pModu le);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem (pArgs, i, pValue);
}
pValue = PyObject_CallOb ject(pFunc, pArgs);
Py_DECREF(pArgs );
if (pValue != NULL) {
printf("Result of call: %ld\n", PyInt_AsLong(pV alue));
Py_DECREF(pValu e);
}
else {
Py_DECREF(pModu le);
PyErr_Print();
fprintf(stderr, "Call failed\n");
return 1;
}
/* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
}
else {
if (PyErr_Occurred ())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_DECREF(pModu le);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
Py_Finalize();
return 0;
}
def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c
Comment