如何把几个python文件打包为一个_如何将所有python代码打包成一个zip文件?

您可以使用常规的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...

Update:pip现在有了-t 开关,它的作用与--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

>>>

>>> from wsgiref.simple_server import demo_app

>>> waitress.serve(demo_app)

serving on http://0.0.0.0:8080

^C>>>

更新:自python 3.5以来,还有zipapp module可以帮助将整个包捆绑到.pyz文件中。对于更复杂的需求pyinstaller、py2exe或py2app可能更适合账单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 PyInstaller 来将多个 Python 文件打包一个可执行文件(.exe)。 以下是一些基本步骤: 1. 安装 PyInstaller。 可以使用以下命令安装 PyInstaller: ``` pip install pyinstaller ``` 2. 创建一个 spec 文件。 spec 文件PyInstaller 的配置文件,它告诉 PyInstaller 如何打包你的项目。你可以使用以下命令生一个默认的 spec 文件: ``` pyinstaller --name=your_app_name your_script.py ``` 这将生一个名为“your_app_name.spec”的文件,其中“your_script.py”是你要打包Python 脚本。 3. 编辑 spec 文件。 打开“your_app_name.spec”文件并编辑它以括你的所有 Python 文件、数据文件和依赖项。你可以在文件中添加以下内容: ``` # 添加其他 Python 文件 a = Analysis(['your_script.py', 'other_script.py'], pathex=['/path/to/your/scripts'], binaries=[], datas=[('/path/to/your/data/file.txt', '.')], hiddenimports=['module1', 'module2']) # 添加其他依赖项 a.datas += [('path/to/dependency.dll', '.')] # 设置应用程序图标 pyz = PYZ(a.pure, a.zipped_data, cipher=None, ) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='your_app_name', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=True ) ``` 4. 打包应用程序。 使用以下命令打包你的应用程序: ``` pyinstaller your_app_name.spec ``` 这将生一个名为“your_app_name.exe”的可执行文件,其中括你的所有 Python 文件、数据文件和依赖项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值