41

在分发应用程序时将所有鸡蛋组合到一个 zip 文件中会很方便,这样您只需要分发一个 zip 文件和一个可执行文件(一些自定义二进制文件,只需启动、加载 zip 文件的主要功能并启动 python关闭或类似)。

我在网上看到过一些关于这样做的讨论,但没有关于如何实际做到这一点的例子。

我知道您可以(如果它是 zip 安全的)将鸡蛋转换为 zip 文件。

我不确定的是:

你能以某种方式将所有鸡蛋合并成一个 zip 文件吗?如果是这样,怎么做?

您将如何从特定的 egg 加载和运行代码?

您将如何确保该 Egg 中的代码可以访问所有依赖项(即 zip 文件中的其他 Egg)?

人们经常问这类问题并得到类似的答案;使用 py2exe。是的,我明白了,这是一种解决方案。这不是我在这里问的问题......

4

7 回答 7

48

您可以使用常规 Python 工具自动完成大部分工作。让我们从干净的 virtualenv 开始。

[zart@feena ~]$ mkdir ziplib-demo
[zart@feena ~]$ cd ziplib-demo
[zart@feena ziplib-demo]$ virtualenv .
New python executable in ./bin/python
Installing setuptools.............done.
Installing pip...............done.

现在让我们安装一组将进入压缩库的软件包。诀窍是强制将它们安装到特定目录中。

(注意:不要在命令行或 pip.conf/pip.ini 中使用 --egg 选项,因为它会破坏文件布局,使其无法在 zip 中导入)

[zart@feena ziplib-demo]$ bin/pip install --install-option --install-lib=$PWD/unpacked waitress
Downloading/unpacking waitress
  Downloading waitress-0.8.5.tar.gz (112kB): 112kB downloaded
  Running setup.py egg_info for package waitress

Requirement already satisfied (use --upgrade to upgrade): setuptools in ./lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg (from waitress)
Installing collected packages: waitress
  Running setup.py install for waitress

    Installing waitress-serve script to /home/zart/ziplib-demo/bin
Successfully installed waitress
Cleaning up...

更新: pip 现在有-t <path>开关,与--install-option --install-lib=.

现在让我们将它们全部打包到一个拉链中

[zart@feena ziplib-demo]$ cd unpacked
[zart@feena unpacked]$ ls
waitress  waitress-0.8.5-py2.7.egg-info
[zart@feena unpacked]$ zip -r9 ../library.zip *
  adding: waitress/ (stored 0%)
  adding: waitress/receiver.py (deflated 71%)
  adding: waitress/server.pyc (deflated 64%)
  adding: waitress/utilities.py (deflated 62%)
  adding: waitress/trigger.pyc (deflated 63%)
  adding: waitress/trigger.py (deflated 61%)
  adding: waitress/receiver.pyc (deflated 60%)
  adding: waitress/adjustments.pyc (deflated 51%)
  adding: waitress/compat.pyc (deflated 56%)
  adding: waitress/adjustments.py (deflated 60%)
  adding: waitress/server.py (deflated 68%)
  adding: waitress/channel.py (deflated 72%)
  adding: waitress/task.pyc (deflated 57%)
  adding: waitress/tests/ (stored 0%)
  adding: waitress/tests/test_regression.py (deflated 63%)
  adding: waitress/tests/test_functional.py (deflated 88%)
  adding: waitress/tests/test_parser.pyc (deflated 76%)
  adding: waitress/tests/test_trigger.pyc (deflated 73%)
  adding: waitress/tests/test_init.py (deflated 72%)
  adding: waitress/tests/test_utilities.pyc (deflated 78%)
  adding: waitress/tests/test_buffers.pyc (deflated 79%)
  adding: waitress/tests/test_trigger.py (deflated 82%)
  adding: waitress/tests/test_buffers.py (deflated 86%)
  adding: waitress/tests/test_runner.py (deflated 75%)
  adding: waitress/tests/test_init.pyc (deflated 69%)
  adding: waitress/tests/__init__.pyc (deflated 21%)
  adding: waitress/tests/support.pyc (deflated 48%)
  adding: waitress/tests/test_utilities.py (deflated 73%)
  adding: waitress/tests/test_channel.py (deflated 87%)
  adding: waitress/tests/test_task.py (deflated 87%)
  adding: waitress/tests/test_functional.pyc (deflated 82%)
  adding: waitress/tests/__init__.py (deflated 5%)
  adding: waitress/tests/test_compat.pyc (deflated 53%)
  adding: waitress/tests/test_receiver.pyc (deflated 79%)
  adding: waitress/tests/test_adjustments.py (deflated 78%)
  adding: waitress/tests/test_adjustments.pyc (deflated 74%)
  adding: waitress/tests/test_server.pyc (deflated 73%)
  adding: waitress/tests/fixtureapps/ (stored 0%)
  adding: waitress/tests/fixtureapps/filewrapper.pyc (deflated 59%)
  adding: waitress/tests/fixtureapps/getline.py (deflated 37%)
  adding: waitress/tests/fixtureapps/nocl.py (deflated 47%)
  adding: waitress/tests/fixtureapps/sleepy.pyc (deflated 44%)
  adding: waitress/tests/fixtureapps/echo.py (deflated 40%)
  adding: waitress/tests/fixtureapps/error.py (deflated 52%)
  adding: waitress/tests/fixtureapps/nocl.pyc (deflated 48%)
  adding: waitress/tests/fixtureapps/getline.pyc (deflated 32%)
  adding: waitress/tests/fixtureapps/writecb.pyc (deflated 42%)
  adding: waitress/tests/fixtureapps/toolarge.py (deflated 37%)
  adding: waitress/tests/fixtureapps/__init__.pyc (deflated 20%)
  adding: waitress/tests/fixtureapps/writecb.py (deflated 50%)
  adding: waitress/tests/fixtureapps/badcl.pyc (deflated 44%)
  adding: waitress/tests/fixtureapps/runner.pyc (deflated 58%)
  adding: waitress/tests/fixtureapps/__init__.py (stored 0%)
  adding: waitress/tests/fixtureapps/filewrapper.py (deflated 74%)
  adding: waitress/tests/fixtureapps/runner.py (deflated 41%)
  adding: waitress/tests/fixtureapps/echo.pyc (deflated 42%)
  adding: waitress/tests/fixtureapps/groundhog1.jpg (deflated 24%)
  adding: waitress/tests/fixtureapps/error.pyc (deflated 48%)
  adding: waitress/tests/fixtureapps/sleepy.py (deflated 42%)
  adding: waitress/tests/fixtureapps/toolarge.pyc (deflated 43%)
  adding: waitress/tests/fixtureapps/badcl.py (deflated 45%)
  adding: waitress/tests/support.py (deflated 52%)
  adding: waitress/tests/test_task.pyc (deflated 78%)
  adding: waitress/tests/test_channel.pyc (deflated 78%)
  adding: waitress/tests/test_regression.pyc (deflated 68%)
  adding: waitress/tests/test_parser.py (deflated 80%)
  adding: waitress/tests/test_server.py (deflated 78%)
  adding: waitress/tests/test_receiver.py (deflated 87%)
  adding: waitress/tests/test_compat.py (deflated 51%)
  adding: waitress/tests/test_runner.pyc (deflated 72%)
  adding: waitress/__init__.pyc (deflated 50%)
  adding: waitress/channel.pyc (deflated 58%)
  adding: waitress/runner.pyc (deflated 54%)
  adding: waitress/buffers.py (deflated 74%)
  adding: waitress/__init__.py (deflated 61%)
  adding: waitress/runner.py (deflated 58%)
  adding: waitress/parser.py (deflated 69%)
  adding: waitress/compat.py (deflated 69%)
  adding: waitress/buffers.pyc (deflated 69%)
  adding: waitress/utilities.pyc (deflated 60%)
  adding: waitress/parser.pyc (deflated 53%)
  adding: waitress/task.py (deflated 72%)
  adding: waitress-0.8.5-py2.7.egg-info/ (stored 0%)
  adding: waitress-0.8.5-py2.7.egg-info/dependency_links.txt (stored 0%)
  adding: waitress-0.8.5-py2.7.egg-info/installed-files.txt (deflated 83%)
  adding: waitress-0.8.5-py2.7.egg-info/top_level.txt (stored 0%)
  adding: waitress-0.8.5-py2.7.egg-info/PKG-INFO (deflated 65%)
  adding: waitress-0.8.5-py2.7.egg-info/not-zip-safe (stored 0%)
  adding: waitress-0.8.5-py2.7.egg-info/SOURCES.txt (deflated 71%)
  adding: waitress-0.8.5-py2.7.egg-info/entry_points.txt (deflated 33%)
  adding: waitress-0.8.5-py2.7.egg-info/requires.txt (deflated 5%)
[zart@feena unpacked]$ cd ..

请注意,这些文件应该在 zip 的顶部,你不能只是zip -r9 library.zip unpacked

检查结果:

[zart@feena ziplib-demo]$ PYTHONPATH=library.zip python
Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import waitress
>>> waitress
<module 'waitress' from '/home/zart/ziplib-demo/library.zip/waitress/__init__.pyc'>
>>>
>>> from wsgiref.simple_server import demo_app
>>> waitress.serve(demo_app)
serving on http://0.0.0.0:8080
^C>>>

更新:从 python 3.5 开始,还有zipapp 模块可以帮助将整个包捆绑到 .pyz 文件中。对于更复杂的需求pyinstallerpy2exepy2app可能更符合要求。

于 2013-07-12T13:57:48.493 回答
20

You could use the zipapp module from the standard library to create executable Python zip archives. It is available from Python 3.5 onwards.

One way to create a bundle is to add a top-level file named __main__.py, which will be the script that Python runs when the zip executable archive is executed.

Suppose your directory structure is now like this:

└── myapp
    ├── __main__.py
    ├── myprog1.py
    └── myprog2.py

If your code has external dependencies (e.g. listed in a file named requirements.txt), install them into the directory using:

pip3 install -r requirements.txt --target myapp/

note 1: This will fill the myapp/ directory with the external dependencies.

note 2: Debian/Ubuntu users may need to use the --system option for pip3, because the Debian/Ubuntu version of pip seems to use --user by default.

Then, create the zip executable archive using:

python3 -m zipapp myapp/

This will create a zip executable archive named myapp.pyz, which you can execute by running:

python3 myapp.pyz

When the zip executable archive is executed, it is __main__.py that is run.

If, in addition to Python scripts, you need to include other data files (e.g. text files, PNG images, etc.) used by the Python scripts, see: python: can executable zip files include data files?

于 2020-02-23T00:08:09.157 回答
11

如果 zip 文件在顶层包含一个 __main__.py[c] 文件,Python 将像执行单个脚本一样执行 zip 文件。然后,包导入还将检查 __main__ 正在从内部执行的 zip 内部。

所以创建你的 setup.py (py_modules = ['__main__']在这里与指定所有包和其他模块一起很重要)。

然后运行python setup.py bdist --format zip以创建 zip 文件。现在,如果您希望它是可执行的,您可以执行以下操作。此时,您可以像执行任何其他 python 脚本一样执行生成的 zip 文件。

阅读本文以提高便利性的 Linux/Mac 用户又迈出了一步(尽管您提到 py2exe 时可能不是您的情况)

echo '#!/usr/bin/env python' > my_executable_zip
cat output_of_setup_py_bdist.zip >> my_executable_zip
chmod +x my_executable_zip

这只是在前面加上 #! 行到 zip 文件,这样当从 shell 运行时,您不需要指定解释器。此时,您可以像系统上的任何其他二进制文件一样执行它,尽管它秘密地是一个充满 python 的 zip 文件。我通常会创建一个生成文件来运行 setup.py,然后进行此转换。

于 2013-10-23T14:05:21.577 回答
1

是的,一个 zip-file/egg 可以提供多个模块,因此您可以将它们组合成一个文件。然而,我高度怀疑这是一个好主意。您仍然需要安装该 zip 文件,并且它可能仍会与其他已安装的版本等发生冲突。

所以首先要问的问题是目标是什么。为什么你只想要一个文件?是为了便于安装,还是为了便于分发,还是为了什么?

只有一个文件不会真正使安装更容易,还有其他更好的方法。您可以让安装程序自动下载并安装依赖项,这很容易做到。

将它们放在一个 zip 文件中仍然意味着您需要扩展该 zip 文件并运行 setup.py,这对用户不是很友好。

所以只有一个文件并不能真正解决很多问题,所以问题是您要解决哪个问题。

于 2013-07-05T10:33:03.123 回答
0

你能以某种方式将所有鸡蛋合并成一个 zip 文件吗?如果是这样,怎么做?

是的你可以。Python 将从 sys.path 中添加的 zip 存档加载(请参阅PEP 273)。如果将所有 python 库放在一个档案中,档案将被视为一个目录。这就是一些 py2exe、bbfreeze 等工具可以用来隔离库的操作。

至于如何安装,这实际上取决于您的鸡蛋的安装方式:pip、easy_install 等。逻辑是检查所有依赖的鸡蛋并收集它们的安装路径,然后将鸡蛋压缩到存档中。

您将如何从特定的 egg 加载和运行代码?

您需要定义加载和运行。如果你在谈论导入模块和包,你不必做任何特别的事情。这是一篇关于该主题的有趣博客文章,包括一些警告将Python 程序打包为可运行的 ZIP 文件

您将如何确保该 Egg 中的代码可以访问所有依赖项(即 zip 文件中的其他 Egg)?

这是内置的,只要鸡蛋不是扩展(即 zip 安全)。另请参阅zipimport

于 2013-07-05T15:27:36.367 回答
0

您可以使用自解压 zip 文件,设置为在从包含鸡蛋的同一个 .exe 文件中解压缩鸡蛋后启动 Python 解释器。

于 2013-07-05T10:30:23.090 回答
0

好吧,可以在你的 {app-home-dir/packages} 中创建你自己的“包/鸡蛋”(例如通过在那里处理鸡蛋)并在 setup.py (setuptools)中配置额外的文件以将其打包为单一发行版(什么是 setup.py?)。请注意,在启动应用程序主功能之前,您需要通过将 {app-home-dir/packages} 添加到 sys.path 来告知 Python 您的外部“包/鸡蛋”的确切位置。这是创建独立包的简单方法..但是这会带来关于依赖项及其版本、Python 模块与 Ansi C 代码混合等方面的危险。

于 2013-07-05T11:07:08.720 回答