embedded python example: PyString_FromString doesnt work?
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
--
http://mail.python.org/mailman/listinfo/python-list
Re: embedded python example: PyString_FromString doesnt work?
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;
"[EMAIL PROTECTED]:~/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
--
http://mail.python.org/mailman/listinfo/python-list
Re: embedded python example: PyString_FromString doesnt work?
On Fri, 25 Mar 2005 11:09:35 +0300, Denis S. Otkidach wrote:
> DH> Calling the program gives an error;
> DH> "[EMAIL PROTECTED]:~/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 :)
--
http://mail.python.org/mailman/listinfo/python-list
