[python-win32] Embedding Python in C++ headaches
Arlis Rose
arlis at endevouraerospace.com
Fri Apr 23 11:49:46 EDT 2004
Okay, this has really got me stuck so I hope someone can help me out :)
Basically I just want to call a Python module from within my MFC code, it
works fine on my main machine where Python has been installed, however I
would like to be able to execute the code on a machine without having to
install python first (so get the python interpreter to run via the .Dll
file). Thus far, whenever I run my MFC executable on a machine without
python installed, it crashes in module python23.Dll. Of course I have NO
idea what is causing it to crash (is it perhaps looking for another file? is
it not possible to run python from C++ using just the module itself and the
dll?).
I am using python23, visual studio .Net, and winXp (if any or all of that is
needed).
Here is an example of my C++ code calling the python module:
Py_Initialize();
pModule=PyImport_ImportModule("IPADDRESS");
pDict=PyModule_GetDict(pModule);
pFunc=PyDict_GetItemString(pDict, "IPADDRESS");
pValue=PyObject_CallFunction(pFunc, "");
char gh[16];
strcpy(gh, PyString_AsString(pValue));
Py_INCREF(pModule);
Py_INCREF(pDict);
Py_INCREF(pFunc);
Py_INCREF(pValue);
Py_Finalize();
When I call this in the C++ code, I simply add "#include <python.h>" and
everything works fine. Now I need to know what I need to include with my
Release executable in order to get the python interpreter to run correctly
(It's so odd because it runs fine on the 3 machines that have python23
pre-installed, but we would prefer not to force an installation of python if
possible). Is it possible that python23.dll requires addition files beyond
the module itself in order to execute? And if this is possible, how can I
determine what those files may be?
The approach I have had to use thus far is to use py2exe to create an
executable of the python code, then I can call the executable from my C++
code using a system call. However, this forces the user to deal with the
command-prompt window popping up while the python script is running (and of
course the user is very much aware that all process's in the program have
stopped while that script is running). I've looked at LOTS of tutorials and
advice on how to embed but can't seem to get anything to work if python
isn't previously installed (is it necessary for it to be Installed???).
Anyways, any help would be much appreciated, either on how to embed in
visual Studio .NET c++ code properly, or even a way to call a python
executable from a C++ program without having a pop-up window (quasi-dos)
flash up on the screen (so I guess would need to use something other than
system to make that call).
More information about the Python-win32
mailing list