[C++-sig] failed in PyImport_Import()

Steven Wyckoff steven.wyckoff at gmail.com
Fri Oct 26 06:49:49 CEST 2007


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 at python.org
> http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list