Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load pyd files from a zip from embedded python

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:

enter image description here

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?

like image 507
Alex van Houten Avatar asked Dec 22 '11 09:12

Alex van Houten


People also ask

Can I read a pyd file?

How to open PYD files. You need a suitable software like Python from Python Software Foundation to open a PYD file. Without proper software you will receive a Windows message "How do you want to open this file?" or "Windows cannot open this file" or a similar Mac/iPhone/Android alert.

What file type is pyd?

pyd file is a library file containing Python code which can be called out to and used by other Python applications. In order to make this library available to other Python programs, it is packaged as a dynamic link library.

Is pyd a DLL?

pyd files are just dll files ready for python importing. To distinguish them from normal dlls, I recommend .


1 Answers

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.

like image 54
theller Avatar answered Sep 24 '22 06:09

theller