C++ & Python - Code Executes and Closes Immediately

Started by
5 comments, last by Liuqahs15 11 years, 6 months ago
I've embedded Python into C++ successfully (without even remotely as much pain and agony as with Lua). Now when I run a simple test program, the console window opens and closes immediately. It definitely executes the way it should with no problems, however I seem to be unable to stop it from closing immediately. I've called system("PAUSE") and added cin >> variable so the program basically has absolutely no choice but to wait for input, yet these things are somehow ignored. I'm on Windows 7 using Visual Studio 2012. Here's the code:


#include <Python.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
int x = 0;
cin >> x;
system("PAUSE");

return 0;
}


Any idea what's going on here? Oddly, when I place that code that's supposed to pause it



int x = 0;
cin >> x;
system("PAUSE");


in BEFORE all the python code, it does actually stop. Even worse: I've used breakpoints. If placed on or before the call to Py_Initialize(), the code stops at the breakpoint. Anywhere else, the breakpoint is never reached. Strangely, VC++ will usually tell you if a breakpoint will never be reached, however it seems to believe that the breakpoints placed after Py_Initialize() will be reached.


UPDATE::

So apparently there's an error occuring. I barely noticed it. What happens is, a message is output "Import Error: No module named site." The program then closes immediately. Any ideas? I'm googling now, and I'll be back with another update if I find an answer, for future readers.
Advertisement
Annoyingly, the python DLL isn't self contained, it depends on several python files to run properly. Which version of Python are you embedding? With the 2.6 series or earlier you can zip up the contents of your Python/Lib directory into a file that you can stick in the Python path. 2.7 calls initsite() before you can set the the python path meaning that you need to place the relevant .py files in just the right spot for Py_Initialize() to work properly.

Annoyingly, the python DLL isn't self contained, it depends on several python files to run properly. Which version of Python are you embedding? With the 2.6 series or earlier you can zip up the contents of your Python/Lib directory into a file that you can stick in the Python path. 2.7 calls initsite() before you can set the the python path meaning that you need to place the relevant .py files in just the right spot for Py_Initialize() to work properly.


Thanks a lot for such a quick response. I'm on 2.7.3. So is there a place where I can find out what .py files to put where? Probably not, right? I'll keep googling and get back with an answer if I find one.

By the way, for clarity:

The error I was getting was "Import Error: No moduled named site."

After I fixed the environment variable in VC++ to PYTHONHOME="E:\Python-2.7.3\" nothing changed. That happens to be the path to python on my hard drive, by the way. If you're reading this, yours might be in C:\Python27\ or something like that
Yeah, in 2.7 initsite() is called before PYTHONHOME is processed. You might find this blog post informative. I basically have given up on embedding 2.7 and just use 2.6.

Yeah, in 2.7 initsite() is called before PYTHONHOME is processed. You might find this blog post informative. I basically have given up on embedding 2.7 and just use 2.6.


I followed your advice and downloaded 2.6.8. Now when I run the code, everything's fine but I get the message 'import site' failed: use -v for traceback. That message is printed in the console. Other than that, it works fine. I'll test it out some more to see if this has any serious rammifications, or if there are any solutions. It's been hard finding any on google.

Thanks again for all your help by the way, it made a huge difference.



For those looking for instructions:

I built the library by opening the PCBuild folder and simply opening the vcproj and building it in visual studio. The python26_d.lib and python26_d.dll files will appear in the PCBuild folder. You'll have to include those in the linker, and that's about it. You can PM me if you're confused.
That error means that it still isn't finding the site.py file. Switching from 2.7 to 2.6 doesn't mean you don't have to supply the file; it just gives you more options on where you can put it and how to tell Python where it is. I generally zip up the .py files I need from the Python Lib directory and put that zip file in sys.path.
That fixed it. In VC++ 2012 I went to Project->Properties->Debugging and under Environment I entered

PYTHONPATH=E:\Python-2.6.8\PyModules.zip;

At first it didn't work because I forgot the semicolon. Thank you so much. You were amazing help!

EDIT: I realized later how awful that explanation was. I copy/pasted the entire contents of E:\Python-2.6.8\Libs\ into a zip folder I made, PyModules.zip, and then set the environment variable that way. So that's how you do it.

This topic is closed to new replies.

Advertisement