load pyd files from a zip from embedded python

蹲街弑〆低调 提交于 2019-12-04 01:21:05

问题


I can load Python modules (.py, .pyc, .pyd) from a zip file by calling "import some_module" from a Python interpreter only after sys.path has been extended to include the zip file and only after I have run

import zipextimporter
zipextimporter.install()

The latter is required for .pyd modules.

I can also load Python .py and .pyc modules from Python embedded in C++. However, in order to also load .pyd modules from embedded Python I added

PyRun_SimpleString("import zipextimporter");

The C++ exe runs beyond this line without error. But the next command

PyRun_SimpleString("zipextimporter.install()");

gives me this error:

Why does zipextimporter.install() crash when Python is embedded?

How can I solve this?

Does it perhaps have to do with the way the C++ code is compiled? I use g++:

g++ embed-simple.cpp -IE:\Python27\include -LE:\Python27\libs -lpython27 -o embed-simple

I saw a link How to link against msvcr90.dll with mingw gcc?

Could that provide a solution? If yes, how should I adjust it, gcc-->g++, since I am running C++ code, not C.

I am running Python 2.7.2 on WinXP.

I don't get the runtime error after a clean install of Python 2.7.2, just this:

Import Error: No module named....

Would it matter the way the embedding C++ script is compiled? I used g++. I also compiled with the Intel compiler, but that gave the same runtime error. Perhaps I should try MS Visual C++.

Or use ctypes to import the pyd?


回答1:


memimporter and zipextimporter are indeed able to load .pyd files from memory/zip-archives without unpacking them to files.

The runtimerror R6034 is caused by the fact that the VC9 runtime library must be loaded via a manifest. Running your code in Python 2.5, whic uses a different C runtime, would most probably succeed. I guess you must embed a manifest referencing the VC9 runtime library in your exe; maybe the py2exe wiki can provide some guidance.




回答2:


PYD files are native DLL files with renamed extension. Loading them relies on operating system facilities and operating system restrictions.

Windows XP, or any OS as far as I know, cannot load DLL files from ZIP files, thus you cannot load PYD files from ZIP files.




回答3:


Which version of python was memimporter.pyd (which is inside the zipextimporter) compiled for? If the python interpreter and pyd don't match you're going to get horrible crashes.

I'm not sure where the mem importer code is, but at a guess I would think that the idea is to insert an import hook which detects a zip-based pyd import and extracts the pyd to a temporary location and call the Python interpreter's standard import on that.




回答4:


this sounds like the same kind of issues I had trying to compile an app with py2exe. see here: http://www.py2exe.org/index.cgi/Tutorial, look at section 5.2. Same thing happens... first time you try to use memimporter, it crashes with a similar error message. Basically, for python 2.6+ you need to have the exact version of the runtime library in the path, and a manifest that points to it. Since you are using embedded python, i don't know how it all works, but maybe that will get you closer.

i'd start by putting the 'correct' version of the runtime dll somewhere, and in your python code, before importing zipextimporter, append the path of the correct runtime to sys.path and see if that fixes it. not sure how you get the manifest in there for an embedded python though. it might need to be included in the parent app's manifest.

edit: BTW, i forgot, we found another way around this issue as well. I forget the exact details, but what happens is that your app loads the default version of the runtime, and then python asks for its version, and it finds one in c:\python{26,27} and it doesn't match. The simplest way to solve this problem is to delete c:\python\msvcr90.dll. Then, python won't hit the local (old) version of the dll which might not work with your app's manifest, and both of them will have to go out and get the current version from the windows directory, which will match.



来源:https://stackoverflow.com/questions/8601950/load-pyd-files-from-a-zip-from-embedded-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!