when im running C code to call python functions, there's error on Py_Initialize() The error is ImportError: No module named site. Ive tried to put Py_SetProgramName(argv[0]) but it doesnt work. The cmd call is cInterfacePython Test.py multiply 3 2 (exe is cInterfacePython)
问题:
回答1:
I had to muck about a bit with the PATH env-var as well as PYTHONPATH to make things work better when embedding.
Py_SetProgramName is not important, it's mostly for internal reference etc...
So, I suggest you find where python is installed locally (this is available in the registry on Windows machines) and use setenv
to set PATH and PYTHONPATH to something appropriate. That would be the python.exe directory for PATH (as in your comment above), as well setting PYTHONPATH to the dir with your own python code and related libraries that you're running from the embedding exe.
Then run Py_Initialize and see if the right thing happens. If you need to modify PYTHONPATH afterward initialization, modify sys.path using PySys_SetPath().
回答2:
I was having the same problem (Windows, both with Visual Studio and MinGW/g++), and I solved it by adding to PYTHONPATH the path to site.py. For some reason, launching python.exe was possible even without it, and sys.path did contain that path (even when PYTHONPATH did not), and I could "import site", but Py_Initialize was not able to do the same thing that python.exe did.