当前位置: 首页 > 面试题库 >

如何将所有python代码捆绑到一个zip文件中?

阙阳
2023-03-14
问题内容

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

我已经看到过一些有关在线执行此操作的讨论,但是没有实际执行此操作的示例。

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

我不确定的是:

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

您如何从特定的鸡蛋加载并运行代码?

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

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


问题答案:

您可以使用常规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>switch,其功能与相同--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文件中。对于更复杂的需求,pyinstaller,py2exe或py2app可能更合适。



 类似资料:
  • 好的,我在分发我的邮件时遇到了一些问题。jar文件。当我只是从NetBeans运行程序时,它可以工作,但是如果我尝试从dist文件夹运行它,它就不工作(没有图像被加载)。 以下是我的程序的层次结构: src/img/随机图像 src/myapp/源文件 我用这个函数加载图像: 在那之后: 在我构建我的项目之后,这些图像在jar文件中,但是它们没有被加载。你知道为什么吗?

  • 问题内容: 我正在尝试将一些插件捆绑到Jenkins.war中,但是我不确定该如何精确。我一直在引用此页面:https : //wiki.jenkins- ci.org/display/JENKINS/Bundling+plugins+with+Jenkins ,但是我对具体细节感到困惑。如果仅将想要的插件中的.hpi文件移到/ WEB-INF / plugins /中,我还需要执行其他哪些步骤才

  • 我们正在努力从Maven迁移到Gradle。不幸的是,我们还有几个战争复盖物要处理。

  • 问题内容: 我做了一个Java应用程序。并将所有类捆绑在jar文件中。.我从netbeans运行项目,我的应用程序运行成功..但是我将我的.jar文件放在另一个地方,然后从那里运行..我没有我的应用程序使用的图标..在代码中,我从项目文件夹中存在的images目录中获取我的图标。 现在,我想知道如何将这些图像文件呈现给最终用户(就像我们呈现.jar文件一样)。 问题答案: 似乎这里有两个问题: 在

  • 我有多个zip文件在一个文件夹和另一个zip文件存在于每一个这些zip文件夹。我想解压第一个和第二个zip文件夹,并创建它们自己的目录。 这是结构 如上所示,在中,我们有多个zip文件,在每个zip文件中,都存在另一个zip文件。我想解压缩,,和到新的文件夹中。我想使用与父zip文件夹相同的名称来放置每个结果。我尝试了以下答案,但这只解压缩第一个文件夹。 顺便说一句,我在Jenkins管道中运行这

  • 我用Flume收集推文,并存储在HDFS上。收集部分工作正常,我可以在我的文件系统中找到我所有的推文。 正如我们所看到的,推文存储在128MB的块中,但只使用几个Ko,这是HDFS的正常行为,如果我错了,请纠正我。 然而,我如何在一个文件中获取所有不同的推文? 这是我使用以下命令运行的conf文件: flume ng agent-n TwitterAgent-f./my flume files/t