如何在不安装的情况下将Python扩展模块打包为可加载的egg?

时间:2021-07-27 23:14:59

I'm in the middle of reworking our build scripts to be based upon the wonderful Waf tool (I did use SCons for ages but its just way too slow).

我正在重新构建我们的构建脚本,以基于美妙的Waf工具(我确实使用了SCons很长时间,但它的速度太慢了)。

Anyway, I've hit the following situation and I cannot find a resolution to it:

无论如何,我遇到了以下情况,我无法找到解决方案:

  • I have a product that depends on a number of previously built egg files.
  • 我有一个产品,取决于许多以前建立的鸡蛋文件。

  • I'm trying to package the product using PyInstaller as part of the build process.
  • 我正在尝试使用PyInstaller打包产品作为构建过程的一部分。

  • I build the dependencies first.
  • 我首先构建依赖项。

  • Next I want to run PyInstaller to package the product that depends on the eggs I built. I need PyInstaller to be able to load those egg files as part of it's packaging process.
  • 接下来我想运行PyInstaller来打包依赖于我构建的蛋的产品。我需要PyInstaller才能将这些egg文件作为其打包过程的一部分加载。

This sounds easy: you work out what PYTHONPATH should be, construct a copy of sys.environ setting the variable up correctly, and then invoke the PyInstaller script using subprocess.Popen passing the previously configured environment as the env argument.

这听起来很容易:你弄清楚PYTHONPATH应该是什么,构造一个正确设置变量的sys.environ副本,然后使用subprocess.Popen调用PyInstaller脚本,将先前配置的环境作为env参数传递。

The problem is that setting PYTHONPATH alone does not seem to be enough if the eggs you are adding are extension modules that are packaged as zipsafe. In this case, it turns out that the embedded libraries are not able to be imported.

问题是,如果要添加的鸡蛋是打包为zipsafe的扩展模块,单独设置PYTHONPATH似乎不够。在这种情况下,事实证明无法导入嵌入式库。

If I unzip the eggs (renaming the directories to .egg), I can import them with no further settings but this is not what I want in this case.

如果我解压缩鸡蛋(将目录重命名为.egg),我可以在没有进一步设置的情况下导入它们,但在这种情况下这不是我想要的。

I can also get the eggs to import from a subshell by doing the following:

我还可以通过执行以下操作从子shell中导入鸡蛋:

  • Setting PYTHONPATH to the directory that contains the egg you want to import (not the path of the egg itself)
  • 将PYTHONPATH设置为包含要导入的egg的目录(不是egg本身的路径)

  • Loading a python shell and using pkg_resources.require to locate the egg.
  • 加载python shell并使用pkg_resources.require找到egg。

Once this has been done, the egg loads as normal. Again, this is not practical because I need to be able to run my python shell in a manner where it is ready to import these eggs from the off.

完成后,鸡蛋正常加载。同样,这是不实际的,因为我需要能够以一种准备从关闭导入这些蛋的方式运行我的python shell。

The dirty option would be to output a wrapper script that took the above actions before calling the real target script but this seems like the wrong thing to do: there must be a better way to do this.

脏选项是输出一个包装脚本,在调用真正的目标脚本之前采取上述操作,但这似乎是错误的做法:必须有更好的方法来做到这一点。

2 个解决方案

#1


Heh, I think this was my bad. The issue appear to have been that the zipsafe flag in setup.py for the extension package was set to False, which appears to affect your ability to treat it as such at all.

嘿,我觉得这是我的坏事。问题似乎是扩展程序包的setup.py中的zipsafe标志设置为False,这似乎会影响您完全处理它的能力。

Now that I've set that to True I can import the egg files, simply by adding each one to the PYTHONPATH.

现在我已将其设置为True我可以导入egg文件,只需将每个文件添加到PYTHONPATH即可。

I hope someone else finds this answer useful one day!

我希望其他人有一天能发现这个答案有用!

#2


Although you have a solution, you could always try "virtualenv" that creates a virtual environment of python where you can install and test Python Packages without messing with the core system python:

虽然你有一个解决方案,你总是可以尝试“virtualenv”创建一个python的虚拟环境,你可以在这里安装和测试Python包,而不会搞乱核心系统python:

http://pypi.python.org/pypi/virtualenv

#1


Heh, I think this was my bad. The issue appear to have been that the zipsafe flag in setup.py for the extension package was set to False, which appears to affect your ability to treat it as such at all.

嘿,我觉得这是我的坏事。问题似乎是扩展程序包的setup.py中的zipsafe标志设置为False,这似乎会影响您完全处理它的能力。

Now that I've set that to True I can import the egg files, simply by adding each one to the PYTHONPATH.

现在我已将其设置为True我可以导入egg文件,只需将每个文件添加到PYTHONPATH即可。

I hope someone else finds this answer useful one day!

我希望其他人有一天能发现这个答案有用!

#2


Although you have a solution, you could always try "virtualenv" that creates a virtual environment of python where you can install and test Python Packages without messing with the core system python:

虽然你有一个解决方案,你总是可以尝试“virtualenv”创建一个python的虚拟环境,你可以在这里安装和测试Python包,而不会搞乱核心系统python:

http://pypi.python.org/pypi/virtualenv