embedded python example: PyString_FromString doesnt work?

D

David Harris

I'm trying the embedded python example here:
http://www.python.org/doc/2.3.2/ext/pure-embedding.html

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_FromString(argv[1]);

.....
}

But it seems to break at the line;
pName = PyString_FromString(argv[1]);

it always seems to return null whatever string is used as a parameter?
Can anyone explain or provide a working example?

TIA
 
B

Brano Zarnovican

Hi David !

I cannot see anything wrong on your code. So, I'm posting my working
example.

Hint: try to determine, why it is returning NULL (the PyErr_Print()
call)

BranoZ


#include <Python.h>

int
main(int argc, char *argv[]) {
PyObject *s;
int ret;

if (argc < 2)
return -1;

Py_Initialize();

s = PyString_FromString(argv[1]);
if (s == NULL) {
PyErr_Print();
return -1;
}
ret = PyObject_Print(s, stdout, 0);
Py_XDECREF(s);
if (ret < 0) {
PyErr_Print();
return -1;
}

return 0;
}

$ cc test_String.c -o test_String -I /usr/include/python2.3 -lpython2.3
$ ./test_String
$ ./test_String hello
'hello'
 
D

David Harris

Hi David !

I cannot see anything wrong on your code. So, I'm posting my working
example.

Hint: try to determine, why it is returning NULL (the PyErr_Print()
call)

BranoZ

OK your example works fine. I inserted it in the original code:
int
main(int argc, char *argv[])
{
PyObject *pName, *s, *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_FromString(argv[1]);
pModule = PyImport_Import(pName);

if (pModule == NULL) {
printf( "import went bang...\n");
PyErr_Print();
return -1;
}
....
}

Te test script (again cut and pasted from the python site example) is just
this:

def multiply(a,b):
print "Will compute", a, "times", b
c = 0
for i in range(0, a):
c = c + b
return c


Calling the program gives an error;
"david@palmer:~/source/python> ./test_String script1.py multiply 4 5
import went bang...
ImportError: No module named script1.py"
script1.py exists and it is in the same directory as the executable so its
not a path error or that kind of stuff.

wierd. does: http://www.python.org/doc/2.3.2/ext/pure-embedding.html work
for you ?

David
 
D

Denis S. Otkidach

On Thu, 24 Mar 2005 21:12:18 +0000 David Harris wrote:

DH> int
DH> main(int argc, char *argv[])
DH> {
DH> PyObject *pName, *s, *pModule, *pDict, *pFunc;
DH> PyObject *pArgs, *pValue;
DH> int i;
DH>
DH> if (argc < 3) {
DH> fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
DH> return 1;
DH> }
DH>
DH>
DH> Py_Initialize();
[...]
DH> Calling the program gives an error;
DH> "david@palmer:~/source/python> ./test_String script1.py multiply 4
DH> 5 import went bang...
DH> ImportError: No module named script1.py"
DH> script1.py exists and it is in the same directory as the executable
DH> so its not a path error or that kind of stuff.

I believe you have to call Py_SetProgramName before Py_Initialize, or
otherwise let Python know that it should look for modules in this
directory. See http://python.org/doc/current/api/embedding.html#l2h-40
 
B

Brano Zarnovican

wierd. does:
http://www.python.org/doc/2.3.­2/ext/pure-embedding.html work
for you ?

Yes. It does.
./test_String script1.py multiply 4 5

Don't run it with the ".py" suffix. The argv[1] is a module name, not a
filename..

Even if you do, it may not find the module. Depending of what you have
in PYTHONPATH. Try also:

export PYTHONPATH=.
I believe you have to call Py_SetProgramName before Py_Initialize,

Yes. This is another way to affect the search path.

BranoZ
 
D

David Harris

DH> Calling the program gives an error;
DH> "david@palmer:~/source/python> ./test_String script1.py multiply 4
DH> 5 import went bang...
DH> ImportError: No module named script1.py"
DH> script1.py exists and it is in the same directory as the executable
DH> so its not a path error or that kind of stuff.

I believe you have to call Py_SetProgramName before Py_Initialize, or
otherwise let Python know that it should look for modules in this
directory. See http://python.org/doc/current/api/embedding.html#l2h-40

excellent!! that was the tip (+ "PySys_SetArgv") the worked ...

This now works:

int
main(int argc, char *argv[])
{
PyObject *pName, *s, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;

Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(argc, argv);
pName = PyString_FromString(argv[1]);
....
}

Thanks a bunch denis :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top