无法将python嵌入到zip库中

时间:2021-01-14 20:29:59

I'm trying to embed python, and provide the dll and a zip of the python libraries and not use any installed python. That is, if a user doesn't have python, I want my code to work using the provided dll/zip.

我正在尝试嵌入python,并提供python库的dll和zip文件,而不使用任何已安装的python。也就是说,如果用户没有python,我希望我的代码使用提供的dll/zip。

This post seems to describe the process, except it isn't working for me.

这篇文章似乎描述了这个过程,除了它对我不起作用。

If I run the following, my code will run as long as I have Python27.dll and a folder named Python27 that contains the DLL and Lib folders.

如果我运行以下代码,我的代码将一直运行到Python27。dll和一个名为Python27的文件夹,包含dll和Lib文件夹。

Py_SetProgramName(argv[0]);  /* optional but recommended */
Py_SetPythonHome("Python27");
Py_Initialize();

If I remove the Python27 folder, the code fails - so I am pulling in the local copy, not any installed python.

如果我删除了Python27文件夹,代码就会失败——所以我要取出本地拷贝,而不是任何已安装的python。

However, if I zip the local Python27 folder, the code stops running, giving "ImportError: No module named site".

但是,如果我压缩了本地Python27文件夹,代码就会停止运行,从而导致“ImportError: No模块命名为site”。

PEP273 makes it sound like this should just work, but everything I've tried has failed.

总统防治艾滋病紧急救援计划让人觉得这应该行得通,但我所做的一切都失败了。

Can anyone shed light on how to get embedded python to run from a zip file?

有人能解释一下如何让嵌入式python从zip文件中运行吗?

Given that there are related questions that have gone unanswered, I think it would be helpful if people would add a comment if they have successfully gotten reading from a zip file working, even if they aren't sure what I might need to fix.

考虑到有一些相关的问题没有得到解答,我认为如果人们能够成功地从zip文件中读取数据,即使他们不确定我可能需要修复什么,也可以添加一条注释,这将是很有帮助的。

That would at least help me understand if I should keep looking for an answer!

这至少能帮助我理解如果我继续寻找答案!

Update: No matter what I try (even with LoadLibrary as suggested), I can run my program from a fully unzipped directory. Any time I remove the directory with DLLs/* and Lib/* and put in Python27.zip instead, I just get

更新:无论我尝试什么(即使建议使用LoadLibrary),我都可以从一个完全解压缩的目录运行我的程序。任何时候,我用DLLs/*和Lib/*删除目录,并放入Python27。拉上拉链,我刚得到

ImportError: No module named site

4 个解决方案

#1


6  

I had two issues.

我有两个问题。

The not well documented 'Py_NoSiteFlag' fixed the

没有很好记录的“Py_NoSiteFlag”固定了。

ImportError: No module named site

first problem. Next, I had to update paths. In the end, I ended up with the following for initialization:

第一个问题。接下来,我必须更新路径。最后,我得到了如下的初始化:

Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_InitializeEx(0);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");

[edit to address question in comments] Once you have the zip'd file loading, you still need to pass files to the python engine. Here's what I use (argument and file existence checks not included).

当您有了zip文件加载后,您仍然需要将文件传递给python引擎。这里是我使用的(参数和文件存在检查不包括在内)。

PyObject* PyFileObject = PyFile_FromString(argv[1], "r");
int res = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), argv[1]);

[/edit]

(/编辑)

#2


2  

I believe your original problem is that you placed the folders in your python zip, when you should place the contents of the DLLs and Lib folders into the python zip.

我认为您最初的问题是您将文件夹放在python zip中,而您应该将dll和Lib文件夹的内容放在python zip中。

Instead of this:

而不是:

python27.zip
  DLLs
    *.*
  Lib
    *.*

You should have:

你应该:

python27.zip
  *.* from DLLs
  *.* from Lib

#3


0  

Note that windows searches for libraries in the executable's path first and only then starts looking in other places. If your python27.dll is always placed in the executable's path and you use LoadLibrary("python27.dll") or bind to the library directly, you just always use the local python version. If the \python27-folder is empty then, your interpreter fails. If the dll is not there, you use the global interpreter.

注意,windows首先在可执行文件的路径中搜索库,然后才开始在其他地方查找。如果你的python27。dll总是放在可执行文件的路径中,您可以使用LoadLibrary(“python27.dll”)或直接绑定到库中,您只需要使用本地python版本。如果\python27文件夹是空的,那么您的解释器将失败。如果dll不在,则使用全局解释器。

Move the dll into a separate folder so windows will only load it if asked for it by LoadLibrary("\p27\python27.dll")

将dll移动到一个单独的文件夹中,以便windows只在LoadLibrary请求时才加载它(“\p27\python27 dll”)

#4


0  

Well, I'd like to post another link here in case someone else encounter similar issue. I have to say that due to bad luck, I happened to download the python 3.3.4, which has a bug when using zipped library, see http://bugs.python.org/issue20852

我想在这里再贴一个链接,以防有人遇到类似的问题。我不得不说,由于运气不好,我碰巧下载了python 3.3.4,它在使用压缩库时有一个bug,请参见http://bugs.python.org/issue20852

#1


6  

I had two issues.

我有两个问题。

The not well documented 'Py_NoSiteFlag' fixed the

没有很好记录的“Py_NoSiteFlag”固定了。

ImportError: No module named site

first problem. Next, I had to update paths. In the end, I ended up with the following for initialization:

第一个问题。接下来,我必须更新路径。最后,我得到了如下的初始化:

Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_InitializeEx(0);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");

[edit to address question in comments] Once you have the zip'd file loading, you still need to pass files to the python engine. Here's what I use (argument and file existence checks not included).

当您有了zip文件加载后,您仍然需要将文件传递给python引擎。这里是我使用的(参数和文件存在检查不包括在内)。

PyObject* PyFileObject = PyFile_FromString(argv[1], "r");
int res = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), argv[1]);

[/edit]

(/编辑)

#2


2  

I believe your original problem is that you placed the folders in your python zip, when you should place the contents of the DLLs and Lib folders into the python zip.

我认为您最初的问题是您将文件夹放在python zip中,而您应该将dll和Lib文件夹的内容放在python zip中。

Instead of this:

而不是:

python27.zip
  DLLs
    *.*
  Lib
    *.*

You should have:

你应该:

python27.zip
  *.* from DLLs
  *.* from Lib

#3


0  

Note that windows searches for libraries in the executable's path first and only then starts looking in other places. If your python27.dll is always placed in the executable's path and you use LoadLibrary("python27.dll") or bind to the library directly, you just always use the local python version. If the \python27-folder is empty then, your interpreter fails. If the dll is not there, you use the global interpreter.

注意,windows首先在可执行文件的路径中搜索库,然后才开始在其他地方查找。如果你的python27。dll总是放在可执行文件的路径中,您可以使用LoadLibrary(“python27.dll”)或直接绑定到库中,您只需要使用本地python版本。如果\python27文件夹是空的,那么您的解释器将失败。如果dll不在,则使用全局解释器。

Move the dll into a separate folder so windows will only load it if asked for it by LoadLibrary("\p27\python27.dll")

将dll移动到一个单独的文件夹中,以便windows只在LoadLibrary请求时才加载它(“\p27\python27 dll”)

#4


0  

Well, I'd like to post another link here in case someone else encounter similar issue. I have to say that due to bad luck, I happened to download the python 3.3.4, which has a bug when using zipped library, see http://bugs.python.org/issue20852

我想在这里再贴一个链接,以防有人遇到类似的问题。我不得不说,由于运气不好,我碰巧下载了python 3.3.4,它在使用压缩库时有一个bug,请参见http://bugs.python.org/issue20852