Hi,<br><br>I have also check that "py_function.py" is in the same directory.<br>i have some doubt <br>1) do we need to install "py_function" module before calling multiply function from that. ( install means python -m py_function)
<br>2) individually multiply function is working fine on python prompt. <br>3) I am using python2.5 headers <br><br>I know some where i had make a mistake. please help me to locate that <br><br>thanks <br>Shrikant<br><br>
<div><span class="gmail_quote">On 10/25/07, <b class="gmail_sendername"><a href="mailto:c++-sig-request@python.org">c++-sig-request@python.org</a></b> <<a href="mailto:c++-sig-request@python.org">c++-sig-request@python.org
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Send C++-sig mailing list submissions to<br> <a href="mailto:c++-sig@python.org">
c++-sig@python.org</a><br><br>To subscribe or unsubscribe via the World Wide Web, visit<br> <a href="http://mail.python.org/mailman/listinfo/c++-sig">http://mail.python.org/mailman/listinfo/c++-sig</a><br>or, via email, send a message with subject or body 'help' to
<br> <a href="mailto:c++-sig-request@python.org">c++-sig-request@python.org</a><br><br>You can reach the person managing the list at<br> <a href="mailto:c++-sig-owner@python.org">c++-sig-owner@python.org</a>
<br><br>When replying, please edit your Subject line so it is more specific<br>than "Re: Contents of C++-sig digest..."<br><br><br>Today's Topics:<br><br> 1. failed in PyImport_Import() (Shrikant Chikhalkar)
<br> 2. Re: failed in PyImport_Import() (Gustavo Carneiro)<br><br><br>----------------------------------------------------------------------<br><br>Message: 1<br>Date: Thu, 25 Oct 2007 12:22:24 +0530<br>From: "Shrikant Chikhalkar" <
<a href="mailto:shrikantvc@gmail.com">shrikantvc@gmail.com</a>><br>Subject: [C++-sig] failed in PyImport_Import()<br>To: <a href="mailto:c++-sig@python.org">c++-sig@python.org</a><br>Message-ID:<br> <<a href="mailto:87de5f460710242352w5b57dc4djbcfba96ccf0f3578@mail.gmail.com">
87de5f460710242352w5b57dc4djbcfba96ccf0f3578@mail.gmail.com</a>><br>Content-Type: text/plain; charset="iso-8859-1"<br><br>Hi,<br><br>i am tring to call python function from 'C' program. i get NULL value return
<br>by PyImport_Import()<br><br>following is the code. code is copied from<br><a href="http://www.codeproject.com/cpp/embedpython_1.asp">http://www.codeproject.com/cpp/embedpython_1.asp</a><br><br>following file is saved as
<br><br>call_function.c<br><br>/************************************************************************************************************/<br><br>// call_function.c - A sample of calling<br>// python functions from C code
<br>//<br>#include <Python.h><br><br>int main(int argc, char *argv[])<br>{<br> PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br><br> if (argc < 3)<br> {<br> printf("Usage: exe_name python_source function_name\n");
<br> return 1;<br> }<br><br> // Initialize the Python Interpreter<br> Py_Initialize();<br><br> // Build the name object<br> pName = PyString_FromString(argv[1]);<br><br> // Load the module object<br>
pModule = PyImport_Import(pName);<br><br> // pDict is a borrowed reference<br> pDict = PyModule_GetDict(pModule);<br><br> // pFunc is also a borrowed reference<br> pFunc = PyDict_GetItemString(pDict, argv[2]);
<br><br> if (PyCallable_Check(pFunc))<br> {<br> PyObject_CallObject(pFunc, NULL);<br> } else<br> {<br> PyErr_Print();<br> }<br><br> // Clean up<br> Py_DECREF(pModule);<br> Py_DECREF(pName);
<br><br> // Finish the Python Interpreter<br> Py_Finalize();<br><br> return 0;<br>}<br><br>/************************************************************************************************************/<br>python file saved as
<br><br>py_function.py<br><br>/************************************************************************************************************/<br><br>'''py_function.py - Python source designed to '''
<br>'''demonstrate the use of python embedding'''<br><br>def multiply():<br> c = 12345*6789<br> print 'The result of 12345 x 6789 :', c<br><br><br> return c<br><br>/************************************************************************************************************/
<br><br>$gcc -g call_function.c -lpython2.5 -ocall_function<br>$./call_function py_function.py multiply<br><br>i get following output<br><br>ImportError: No module named py_function<br>Failed to load "py_function"
<br><br>when i kdbg(debug) it i vet NULL value returned from PyImport_Import()..<br><br>can any one please find out the error that i am doing.<br><br>--<br>Shrikant<br>-------------- next part --------------<br>An HTML attachment was scrubbed...
<br>URL: <a href="http://mail.python.org/pipermail/c++-sig/attachments/20071025/e3ca2dfd/attachment-0001.htm">http://mail.python.org/pipermail/c++-sig/attachments/20071025/e3ca2dfd/attachment-0001.htm</a><br><br>------------------------------
<br><br>Message: 2<br>Date: Thu, 25 Oct 2007 08:21:19 +0100<br>From: "Gustavo Carneiro" <<a href="mailto:gjcarneiro@gmail.com">gjcarneiro@gmail.com</a>><br>Subject: Re: [C++-sig] failed in PyImport_Import()
<br>To: "Development of Python/C++ integration" <<a href="mailto:c++-sig@python.org">c++-sig@python.org</a>><br>Message-ID:<br> <<a href="mailto:a467ca4f0710250021x50e0a9c8m5ad849e9b9e3d5b1@mail.gmail.com">
a467ca4f0710250021x50e0a9c8m5ad849e9b9e3d5b1@mail.gmail.com</a>><br>Content-Type: text/plain; charset="utf-8"<br><br>On 25/10/2007, Shrikant Chikhalkar <<a href="mailto:shrikantvc@gmail.com">shrikantvc@gmail.com
</a>> wrote:<br>><br>> Hi,<br>><br>> i am tring to call python function from 'C' program. i get NULL value<br>> return by PyImport_Import()<br>><br>> following is the code. code is copied from
<br>> <a href="http://www.codeproject.com/cpp/embedpython_1.asp">http://www.codeproject.com/cpp/embedpython_1.asp</a><br>><br>> following file is saved as<br>><br>> call_function.c<br>><br>> /************************************************************************************************************/
<br>><br>><br>> // call_function.c - A sample of calling<br>> // python functions from C code<br>><br>> //<br>> #include <Python.h><br>><br>> int main(int argc, char *argv[])<br>> {<br>
><br>> PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br>><br>> if (argc < 3)<br>> {<br>> printf("Usage: exe_name python_source function_name\n"<br>> );<br>> return 1;
<br>> }<br>><br>> // Initialize the Python Interpreter<br>> Py_Initialize();<br>><br>><br>> // Build the name object<br>> pName = PyString_FromString(argv[1]);<br>><br>> // Load the module object
<br>> pModule = PyImport_Import(pName);<br>><br>><br>> // pDict is a borrowed reference<br>> pDict = PyModule_GetDict(pModule);<br>><br>> // pFunc is also a borrowed reference<br>> pFunc = PyDict_GetItemString(pDict, argv[
<br>> 2]);<br>><br>> if (PyCallable_Check(pFunc))<br>> {<br>> PyObject_CallObject(pFunc, NULL);<br>> } else<br>> {<br>><br>> PyErr_Print();<br>> }<br>><br>
> // Clean up<br>> Py_DECREF(pModule);<br>> Py_DECREF(pName);<br>><br>> // Finish the Python Interpreter<br>><br>> Py_Finalize();<br>><br>> return 0;<br>> }<br>><br>
> /************************************************************************************************************/<br>><br>> python file saved as<br>><br>> py_function.py<br>><br>><br>> /************************************************************************************************************/
<br>><br>> '''py_function.py - Python source designed to '''<br>> '''demonstrate the use of python embedding'''<br>><br>> def multiply():<br>> c = 12345*6789
<br>> print 'The result of 12345 x 6789 :', c<br>><br>><br>><br>> return c<br>><br>><br>> /************************************************************************************************************/
<br>><br>> $gcc -g call_function.c -lpython2.5 -ocall_function<br>> $./call_function py_function.py multiply<br><br><br>Shouldn't this be:<br> $./call_function py_function multiply<br><br>I.e., you the module name should not have a .py extension.
<br><br>i get following output<br>><br>> ImportError: No module named py_function<br>> Failed to load "py_function"<br>><br>> when i kdbg(debug) it i vet NULL value returned from PyImport_Import()..
<br>><br>> can any one please find out the error that i am doing.<br>><br>> --<br>> Shrikant<br>><br>><br>> _______________________________________________<br>> C++-sig mailing list<br>> <a href="mailto:C++-sig@python.org">
C++-sig@python.org</a><br>> <a href="http://mail.python.org/mailman/listinfo/c++-sig">http://mail.python.org/mailman/listinfo/c++-sig</a><br>><br>><br><br><br>--<br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit
<br>"The universe is always one step beyond logic." -- Frank Herbert<br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: <a href="http://mail.python.org/pipermail/c++-sig/attachments/20071025/0fc3b1ea/attachment-0001.htm">
http://mail.python.org/pipermail/c++-sig/attachments/20071025/0fc3b1ea/attachment-0001.htm</a><br><br>------------------------------<br><br>_______________________________________________<br>C++-sig mailing list<br><a href="mailto:C++-sig@python.org">
C++-sig@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/c++-sig">http://mail.python.org/mailman/listinfo/c++-sig</a><br><br><br>End of C++-sig Digest, Vol 51, Issue 25<br>***************************************
<br></blockquote></div><br><br clear="all"><br>-- <br>Shrikant Chikhalkar<br>+91 9850991554