failed in PyImport_Import()
Hi, i am tring to call python function from 'C' program. i get NULL value return by PyImport_Import() following is the code. code is copied from http://www.codeproject.com/cpp/embedpython_1.asp following file is saved as call_function.c /************************************************************************************************************/ // call_function.c - A sample of calling // python functions from C code // #include <Python.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc, *pValue; if (argc < 3) { printf("Usage: exe_name python_source function_name\n"); return 1; } // Initialize the Python Interpreter Py_Initialize(); // Build the name object pName = PyString_FromString(argv[1]); // Load the module object pModule = PyImport_Import(pName); // pDict is a borrowed reference pDict = PyModule_GetDict(pModule); // pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, argv[2]); if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else { PyErr_Print(); } // Clean up Py_DECREF(pModule); Py_DECREF(pName); // Finish the Python Interpreter Py_Finalize(); return 0; } /************************************************************************************************************/ python file saved as py_function.py /************************************************************************************************************/ '''py_function.py - Python source designed to ''' '''demonstrate the use of python embedding''' def multiply(): c = 12345*6789 print 'The result of 12345 x 6789 :', c return c /************************************************************************************************************/ $gcc -g call_function.c -lpython2.5 -ocall_function $./call_function py_function.py multiply i get following output ImportError: No module named py_function Failed to load "py_function" when i kdbg(debug) it i vet NULL value returned from PyImport_Import().. can any one please find out the error that i am doing. -- Shrikant
On 25/10/2007, Shrikant Chikhalkar <shrikantvc@gmail.com> wrote:
Hi,
i am tring to call python function from 'C' program. i get NULL value return by PyImport_Import()
following is the code. code is copied from http://www.codeproject.com/cpp/embedpython_1.asp
following file is saved as
call_function.c
/************************************************************************************************************/
// call_function.c - A sample of calling // python functions from C code
// #include <Python.h>
int main(int argc, char *argv[]) {
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
if (argc < 3) { printf("Usage: exe_name python_source function_name\n" ); return 1; }
// Initialize the Python Interpreter Py_Initialize();
// Build the name object pName = PyString_FromString(argv[1]);
// Load the module object pModule = PyImport_Import(pName);
// pDict is a borrowed reference pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, argv[ 2]);
if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else {
PyErr_Print(); }
// Clean up Py_DECREF(pModule); Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0; }
/************************************************************************************************************/
python file saved as
py_function.py
/************************************************************************************************************/
'''py_function.py - Python source designed to ''' '''demonstrate the use of python embedding'''
def multiply(): c = 12345*6789 print 'The result of 12345 x 6789 :', c
return c
/************************************************************************************************************/
$gcc -g call_function.c -lpython2.5 -ocall_function $./call_function py_function.py multiply
Shouldn't this be: $./call_function py_function multiply I.e., you the module name should not have a .py extension. i get following output
ImportError: No module named py_function Failed to load "py_function"
when i kdbg(debug) it i vet NULL value returned from PyImport_Import()..
can any one please find out the error that i am doing.
-- Shrikant
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
-- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
Based on reading the code, I cannot see why it would be failing to create the module. Having gone through a similar process not too long ago, there are a couple gotchas that I ran into. First is the .py in the module name: you need it in the file name, and not in the module name (just like importing within python). Another is that depending on your debugger, it may be running the program in a different location than the executable is located (for example VS does this I know). You had mentioned that your py_function works properly in a python prompt but just as a heads up, we had problems where a syntax error in the python file would cause cryptic module loading errors. You may also want to check that there is not an old version of the .pyc file sitting around confusing things. It looks like you are using PyErr_Print() and getting some info back from it, I found that to be very helpful, even more so once the scripts were running. Python should look for a module in the directory where it is being run from, however, if you think that the issue is that python is not finding your file, you can add an arbitrary directory on your computer to the module search path within your program: PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\"your\path\here")"); You could also try using PyRun_SimpleString("import py_function") to try importing the module just to try to isolate the issue. I hope that your problem is the same as one of ours and good luck! Steve Wyckoff Shrikant Chikhalkar wrote:
Hi,
i am tring to call python function from 'C' program. i get NULL value return by PyImport_Import()
following is the code. code is copied from http://www.codeproject.com/cpp/embedpython_1.asp
following file is saved as
call_function.c
/************************************************************************************************************/
// call_function.c - A sample of calling // python functions from C code
// #include <Python.h>
int main(int argc, char *argv[]) {
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
if (argc < 3) { printf("Usage: exe_name python_source function_name\n" ); return 1; }
// Initialize the Python Interpreter Py_Initialize();
// Build the name object pName = PyString_FromString(argv[1]);
// Load the module object pModule = PyImport_Import(pName);
// pDict is a borrowed reference pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference pFunc = PyDict_GetItemString(pDict, argv[ 2]);
if (PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } else {
PyErr_Print(); }
// Clean up Py_DECREF(pModule); Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0; }
/************************************************************************************************************/
python file saved as
py_function.py
/************************************************************************************************************/
'''py_function.py - Python source designed to ''' '''demonstrate the use of python embedding'''
def multiply(): c = 12345*6789 print 'The result of 12345 x 6789 :', c
return c
/************************************************************************************************************/
$gcc -g call_function.c -lpython2.5 -ocall_function $./call_function py_function.py multiply
i get following output
ImportError: No module named py_function Failed to load "py_function"
when i kdbg(debug) it i vet NULL value returned from PyImport_Import()..
can any one please find out the error that i am doing.
-- Shrikant
------------------------------------------------------------------------
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
Oh My goodness, I've been having the same problem and I have been trying to figure this out the entire day! and This solved it for me.. What a weird weird problem... Thank you so much Steaven Wyckoff! You are the life saver!! Steven Wyckoff wrote:
PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\"your/path/here\")");
-- View this message in context: http://www.nabble.com/failed-in-PyImport_Import%28%29-tf4688986.html#a135345... Sent from the Python - c++-sig mailing list archive at Nabble.com.
participants (4)
-
Gustavo Carneiro -
jjiinnnn -
Shrikant Chikhalkar -
Steven Wyckoff