从嵌入式python的zip加载pyd文件 [英] load pyd files from a zip from embedded python

查看:173
本文介绍了从嵌入式python的zip加载pyd文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在将sys.path扩展为包括zip文件之后,才可以通过从Python解释器调用 import some_module
从zip文件中加载Python模块(.py,.pyc,.pyd)。 b $ b并且只有在我运行后

  import zipextimporter 
zipextimporter.install()

后者是.pyd模块所必需的。



我也可以从嵌入在C ++中的Python加载Python .py和.pyc模块。但是,为了也从嵌入式Python加载.pyd模块,我添加了

  PyRun_SimpleString( import zipextimporter); 

C ++ exe超出此行运行且没有错误。但是下一条命令

  PyRun_SimpleString( zipextimporter.install()); 

给我这个错误:



< img src = https://i.stack.imgur.com/3iYeR.png alt =在此处输入图片描述>



为什么zipextimporter.install( )在嵌入Python时崩溃?



我该如何解决?



也许与C ++代码
的编译方式有关吗?我使用g ++:



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



我看到了一个链接
如何使用mingw gcc与msvcr90.dll链接?



<可以提供解决方案吗?
如果是,我应该如何调整gcc-> g ++,因为我正在运行C ++代码,而不是C。



我正在运行Python 2.7。在WinXP上为2。



在全新安装Python 2.7.2之后,我没有得到运行时错误,只是这样:


导入错误:没有名为...的模块。


是否会影响方式嵌入的C ++脚本是否已编译?我用的是g ++。我也用英特尔编译器编译了
,但是给出了相同的运行时错误。也许我应该尝试MS Visual C ++。



还是使用ctypes导入pyd?

解决方案

memimporter和zipextimporter确实能够从内存/ zip归档文件中加载.pyd文件,而无需将它们解压缩到文件中。



runtimerror R6034是由于VC9运行时库必须通过清单加载。如果使用不同的C运行时在Python 2.5中运行代码,则很可能会成功。我猜您必须在自己的exe中嵌入一个引用VC9运行时库的清单。也许py2exe Wiki可以提供一些指导。


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?

解决方案

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.

这篇关于从嵌入式python的zip加载pyd文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆