Hi,<br><br>I have also check that &quot;py_function.py&quot; is in the same directory.<br>i have some doubt <br>1) do we need to install &quot;py_function&quot; module before calling multiply function from that. ( install means python -m&nbsp; 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> &lt;<a href="mailto:c++-sig-request@python.org">c++-sig-request@python.org
</a>&gt; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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 &#39;help&#39; to
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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 &quot;Re: Contents of C++-sig digest...&quot;<br><br><br>Today&#39;s Topics:<br><br>&nbsp;&nbsp; 1. failed in PyImport_Import() (Shrikant Chikhalkar)
<br>&nbsp;&nbsp; 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: &quot;Shrikant Chikhalkar&quot; &lt;
<a href="mailto:shrikantvc@gmail.com">shrikantvc@gmail.com</a>&gt;<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;<a href="mailto:87de5f460710242352w5b57dc4djbcfba96ccf0f3578@mail.gmail.com">
87de5f460710242352w5b57dc4djbcfba96ccf0f3578@mail.gmail.com</a>&gt;<br>Content-Type: text/plain; charset=&quot;iso-8859-1&quot;<br><br>Hi,<br><br>i am tring to call python function from &#39;C&#39; 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 &lt;Python.h&gt;<br><br>int main(int argc, char *argv[])<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if (argc &lt; 3)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Usage: exe_name python_source function_name\n&quot;);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// Initialize the Python Interpreter<br>&nbsp;&nbsp;&nbsp;&nbsp;Py_Initialize();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// Build the name object<br>&nbsp;&nbsp;&nbsp;&nbsp;pName = PyString_FromString(argv[1]);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// Load the module object<br>
&nbsp;&nbsp;&nbsp;&nbsp;pModule = PyImport_Import(pName);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// pDict is a borrowed reference<br>&nbsp;&nbsp;&nbsp;&nbsp;pDict = PyModule_GetDict(pModule);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// pFunc is also a borrowed reference<br>&nbsp;&nbsp;&nbsp;&nbsp;pFunc = PyDict_GetItemString(pDict, argv[2]);
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if (PyCallable_Check(pFunc))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PyObject_CallObject(pFunc, NULL);<br>&nbsp;&nbsp;&nbsp;&nbsp;} else<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PyErr_Print();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// Clean up<br>&nbsp;&nbsp;&nbsp;&nbsp;Py_DECREF(pModule);<br>&nbsp;&nbsp;&nbsp;&nbsp;Py_DECREF(pName);
<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// Finish the Python Interpreter<br>&nbsp;&nbsp;&nbsp;&nbsp;Py_Finalize();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>}<br><br>/************************************************************************************************************/<br>python file saved as
<br><br>py_function.py<br><br>/************************************************************************************************************/<br><br>&#39;&#39;&#39;py_function.py - Python source designed to &#39;&#39;&#39;
<br>&#39;&#39;&#39;demonstrate the use of python embedding&#39;&#39;&#39;<br><br>def multiply():<br>&nbsp;&nbsp;&nbsp;&nbsp;c = 12345*6789<br>&nbsp;&nbsp;&nbsp;&nbsp;print &#39;The result of 12345 x 6789 :&#39;, c<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;return c<br><br>/************************************************************************************************************/
<br><br>$gcc -g call_function.c&nbsp;&nbsp;-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 &quot;py_function&quot;
<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: &quot;Gustavo Carneiro&quot; &lt;<a href="mailto:gjcarneiro@gmail.com">gjcarneiro@gmail.com</a>&gt;<br>Subject: Re: [C++-sig] failed in PyImport_Import()
<br>To: &quot;Development of Python/C++ integration&quot; &lt;<a href="mailto:c++-sig@python.org">c++-sig@python.org</a>&gt;<br>Message-ID:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;<a href="mailto:a467ca4f0710250021x50e0a9c8m5ad849e9b9e3d5b1@mail.gmail.com">
a467ca4f0710250021x50e0a9c8m5ad849e9b9e3d5b1@mail.gmail.com</a>&gt;<br>Content-Type: text/plain; charset=&quot;utf-8&quot;<br><br>On 25/10/2007, Shrikant Chikhalkar &lt;<a href="mailto:shrikantvc@gmail.com">shrikantvc@gmail.com
</a>&gt; wrote:<br>&gt;<br>&gt; Hi,<br>&gt;<br>&gt; i am tring to call python function from &#39;C&#39; program. i get NULL value<br>&gt; return by PyImport_Import()<br>&gt;<br>&gt; following is the code. code is copied from
<br>&gt; <a href="http://www.codeproject.com/cpp/embedpython_1.asp">http://www.codeproject.com/cpp/embedpython_1.asp</a><br>&gt;<br>&gt; following file is saved as<br>&gt;<br>&gt; call_function.c<br>&gt;<br>&gt; /************************************************************************************************************/
<br>&gt;<br>&gt;<br>&gt; // call_function.c - A sample of calling<br>&gt; // python functions from C code<br>&gt;<br>&gt; //<br>&gt; #include &lt;Python.h&gt;<br>&gt;<br>&gt; int main(int argc, char *argv[])<br>&gt; {<br>
&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; if (argc &lt; 3)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Usage: exe_name python_source function_name\n&quot;<br>&gt; );<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // Initialize the Python Interpreter<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Py_Initialize();<br>&gt;<br>&gt;<br>&gt; // Build the name object<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; pName = PyString_FromString(argv[1]);<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // Load the module object
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; pModule = PyImport_Import(pName);<br>&gt;<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // pDict is a borrowed reference<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; pDict = PyModule_GetDict(pModule);<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // pFunc is also a borrowed reference<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; pFunc = PyDict_GetItemString(pDict, argv[
<br>&gt; 2]);<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; if (PyCallable_Check(pFunc))<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyObject_CallObject(pFunc, NULL);<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; } else<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyErr_Print();<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&gt;<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; // Clean up<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pModule);<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Py_DECREF(pName);<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // Finish the Python Interpreter<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Py_Finalize();<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; return 0;<br>&gt; }<br>&gt;<br>
&gt; /************************************************************************************************************/<br>&gt;<br>&gt; python file saved as<br>&gt;<br>&gt; py_function.py<br>&gt;<br>&gt;<br>&gt; /************************************************************************************************************/
<br>&gt;<br>&gt; &#39;&#39;&#39;py_function.py - Python source designed to &#39;&#39;&#39;<br>&gt; &#39;&#39;&#39;demonstrate the use of python embedding&#39;&#39;&#39;<br>&gt;<br>&gt; def multiply():<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; c = 12345*6789
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; print &#39;The result of 12345 x 6789 :&#39;, c<br>&gt;<br>&gt;<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; return c<br>&gt;<br>&gt;<br>&gt; /************************************************************************************************************/
<br>&gt;<br>&gt; $gcc -g call_function.c&nbsp;&nbsp;-lpython2.5 -ocall_function<br>&gt; $./call_function py_function.py multiply<br><br><br>Shouldn&#39;t this be:<br>&nbsp;&nbsp; $./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>&gt;<br>&gt; ImportError: No module named py_function<br>&gt; Failed to load &quot;py_function&quot;<br>&gt;<br>&gt; when i kdbg(debug) it i vet NULL value returned from PyImport_Import()..
<br>&gt;<br>&gt; can any one please find out the error that i am doing.<br>&gt;<br>&gt; --<br>&gt; Shrikant<br>&gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; C++-sig mailing list<br>&gt; <a href="mailto:C++-sig@python.org">
C++-sig@python.org</a><br>&gt; <a href="http://mail.python.org/mailman/listinfo/c++-sig">http://mail.python.org/mailman/listinfo/c++-sig</a><br>&gt;<br>&gt;<br><br><br>--<br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit
<br>&quot;The universe is always one step beyond logic.&quot; -- 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