By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 2,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

embedded python example: PyString_FromString doesnt work?

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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'

Jul 18 '05 #2

P: n/a
On Thu, 24 Mar 2005 06:39:47 -0800, Brano Zarnovican wrote:
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
Jul 18 '05 #3

P: n/a
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

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 18 '05 #4

P: n/a
> wierd. does:
http://www.python.org/doc/2.3.*2/ext...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

Jul 18 '05 #5

P: n/a
On Fri, 25 Mar 2005 11:09:35 +0300, Denis S. Otkidach wrote:
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 :)
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.