初始化时嵌入 python 错误
当我运行 C 代码调用 python 函数时,Py_Initialize() 出现错误,错误为 ImportError: No module named site.我尝试放置 Py_SetProgramName(argv[0]) 但它不起作用。 cmd调用是cInterfacePython Test.py乘以3 2(exe是cInterfacePython)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不得不对 PATH env-var 和 PYTHONPATH 进行一些修改,以使嵌入时工作得更好。
Py_SetProgramName 并不重要,它主要用于内部参考等...
因此,我建议您找到本地安装 python 的位置(这可以在 Windows 计算机上的注册表中找到)并使用
setenv
设置 PATH 和PYTHONPATH 到适当的东西。这将是 PATH 的 python.exe 目录(如上面的评论所示),以及使用您自己的 python 代码和从嵌入 exe 运行的相关库将 PYTHONPATH 设置为该目录。然后运行 Py_Initialize 并查看是否发生了正确的事情。如果需要在初始化后修改 PYTHONPATH,请使用 PySys_SetPath() 修改 sys.path。
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().
我遇到了同样的问题(Windows,都使用 Visual Studio 和 MinGW/g++),我通过将 site.py 的路径添加到 PYTHONPATH 来解决它。
由于某种原因,即使没有它,启动 python.exe 也是可能的,并且 sys.path 确实包含该路径(即使 PYTHONPATH 没有),并且我可以“导入站点”,但 Py_Initialize 无法执行与 python 相同的操作.exe 做到了。
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.