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">
http://www.codeproject.com/cpp/embedpython_1.asp</a><br><br>following file is saved as<span id="intelliTXT"><pre style="margin-top: 0pt;" id="pre0"><span class="cpp-comment">call_function.c</span></pre></span>/************************************************************************************************************/
<br><span id="intelliTXT"><pre style="margin-top: 0pt;" id="pre0"><span class="cpp-comment">// call_function.c - A sample of calling </span><br><span class="cpp-comment">// python functions from C code</span><br><span class="cpp-comment">
// </span><br><span class="cpp-preprocessor">#include &lt;Python.h&gt;</span><br><br><span class="cpp-keyword">int</span> main(<span class="cpp-keyword">int</span> argc, <span class="cpp-keyword">char</span> *argv[])<br>{
<br>    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;<br><br>    <span class="cpp-keyword">if</span> (argc &lt; <span class="cpp-literal">3</span>) <br>    {<br>        printf(<span class="cpp-string">&quot;Usage: exe_name python_source function_name\n&quot;
</span>);<br>        <span class="cpp-keyword">return</span> <span class="cpp-literal">1</span>;<br>    }<br><br>    <span class="cpp-comment">// Initialize the Python Interpreter</span><br>    Py_Initialize();<br><br>    
<span class="cpp-comment">// Build the name object</span><br>    pName = PyString_FromString(argv[<span class="cpp-literal">1</span>]);<br><br>    <span class="cpp-comment">// Load the module object</span><br>    pModule = PyImport_Import(pName);
<br><br>    <span class="cpp-comment">// pDict is a borrowed reference </span><br>    pDict = PyModule_GetDict(pModule);<br><br>    <span class="cpp-comment">// pFunc is also a borrowed reference </span><br>    pFunc = PyDict_GetItemString(pDict, argv[
<span class="cpp-literal">2</span>]);<br><br>    <span class="cpp-keyword">if</span> (PyCallable_Check(pFunc)) <br>    {<br>        PyObject_CallObject(pFunc, NULL);<br>    } <span class="cpp-keyword">else</span> <br>    {
<br>        PyErr_Print();<br>    }<br><br>    <span class="cpp-comment">// Clean up</span><br>    Py_DECREF(pModule);<br>    Py_DECREF(pName);<br><br>    <span class="cpp-comment">// Finish the Python Interpreter</span><br>
    Py_Finalize();<br><br>    <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;<br>}</pre></span>/************************************************************************************************************/
<br>python file saved as&nbsp; <span id="intelliTXT"><pre lang="Python">py_function.py</pre></span>/************************************************************************************************************/<br><span id="intelliTXT">
<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>    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<br><br>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>Shrikant<br><br>