On 25/10/2007, <b class="gmail_sendername">Shrikant Chikhalkar</b> &lt;<a href="mailto:shrikantvc@gmail.com">shrikantvc@gmail.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>i am tring to call python function from &#39;C&#39; program. i get NULL value return by PyImport_Import()<br><br>following is the code. code is copied from <a href="http://www.codeproject.com/cpp/embedpython_1.asp" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

http://www.codeproject.com/cpp/embedpython_1.asp</a><br><br>following file is saved as<span><pre style="margin-top: 0pt;"><span>call_function.c</span></pre></span>/************************************************************************************************************/
<br><span><pre style="margin-top: 0pt;"><span>// call_function.c - A sample of calling </span><br><span>// python functions from C code</span><br><span>
// </span><br><span>#include &lt;Python.h&gt;</span><br><br><span>int</span> main(<span>int</span> argc, <span>char</span> *argv[])<br>{<br><br>    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br><br>    <span>if</span>
 (argc &lt; <span>3</span>) <br>    {<br>        printf(<span>&quot;Usage: exe_name python_source function_name\n&quot;<br></span>);<br>        <span>return</span> <span>1</span>;<br>    }<br><br>    <span>// Initialize the Python Interpreter
</span><br>    Py_Initialize();<br><br>    <br><span>// Build the name object</span><br>    pName = PyString_FromString(argv[<span>1</span>]);<br><br>    <span>// Load the module object</span><br>    pModule = PyImport_Import(pName);
<br><br><br>    <span>// pDict is a borrowed reference </span><br>    pDict = PyModule_GetDict(pModule);<br><br>    <span>// pFunc is also a borrowed reference </span><br>    pFunc = PyDict_GetItemString(pDict, argv[<br><span>
2</span>]);<br><br>    <span>if</span> (PyCallable_Check(pFunc)) <br>    {<br>        PyObject_CallObject(pFunc, NULL);<br>    } <span>else</span> <br>    {<br><br>        PyErr_Print();<br>    }<br><br>    <span>// Clean up
</span><br>    Py_DECREF(pModule);<br>    Py_DECREF(pName);<br><br>    <span>// Finish the Python Interpreter</span><br>
    Py_Finalize();<br><br>    <span>return</span> <span>0</span>;<br>}</pre></span>/************************************************************************************************************/
<br>python file saved as&nbsp; <span><pre lang="Python">py_function.py</pre></span>/************************************************************************************************************/<br><span>
<pre lang="Python">&#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>    c = 12345*6789<br>    print &#39;The result of 12345 x 6789 :&#39;, c
<br><br><br><br>    return c</pre></span>/************************************************************************************************************/<br clear="all"><br>$gcc -g call_function.c&nbsp; -lpython2.5 -ocall_function
<br>
$./call_function py_function.py multiply</blockquote><div><br>Shouldn&#39;t this be:<br>&nbsp;&nbsp; $./call_function py_function multiply<br>&nbsp;<br>I.e., you the module name should not have a .py extension.<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
i get following output<br><br><span style="font-style: italic;">ImportError: No module named py_function</span><br style="font-style: italic;"><span style="font-style: italic;">
Failed to load &quot;py_function&quot;</span><br style="font-style: italic;"><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><span class="sg">Shrikant<br><br>
</span><br>_______________________________________________<br>C++-sig mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:C++-sig@python.org">C++-sig@python.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://mail.python.org/mailman/listinfo/c++-sig" target="_blank">
http://mail.python.org/mailman/listinfo/c++-sig</a><br><br></blockquote></div><br><br clear="all"><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