Python 打包输出exe

Python 打包输出exe

python 中可以使用第三方库pyinstaller 来将Python代码打包成可执行程序exe.

pyinstaller 会将python解释器,依赖的库,以及代码打包成一个单独的可执行文件.这样即使没有安装python 也可以运行程序.

1: 安装pyinstaller

pip install pyinstaller

2: 打包

执行下面命令打包:

 pyinstaller deviceInfo.py

输出如下:

48 INFO: PyInstaller: 5.0.1
48 INFO: Python: 3.8.10
57 INFO: Platform: Linux-5.4.0-174-generic-x86_64-with-glibc2.29
58 INFO: wrote /home/zh/workSpace/python/Test1/venv/deviceInfo.spec
60 INFO: UPX is not available.
62 INFO: Extending PYTHONPATH with paths
['/home/zh/workSpace/python/Test1/venv']
365 INFO: checking Analysis
365 INFO: Building Analysis because Analysis-00.toc is non existent
365 INFO: Initializing module dependency graph...
366 INFO: Caching module graph hooks...
370 WARNING: Several hooks defined for module 'numpy'. Please take care they do not conflict.
371 INFO: Analyzing base_library.zip ...
2231 INFO: Processing pre-find module path hook distutils from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
2232 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.8'
4534 INFO: Caching module dependency graph...
4647 INFO: running Analysis Analysis-00.toc
4669 INFO: Analyzing /home/zh/workSpace/python/Test1/venv/deviceInfo.py
4672 INFO: Processing module hooks...
4672 INFO: Loading module hook 'hook-sysconfig.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4681 INFO: Loading module hook 'hook-distutils.util.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4682 INFO: Loading module hook 'hook-distutils.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4682 INFO: Loading module hook 'hook-_tkinter.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4751 INFO: checking Tree
4751 INFO: Building Tree because Tree-00.toc is non existent
4751 INFO: Building Tree Tree-00.toc
4764 INFO: checking Tree
4764 INFO: Building Tree because Tree-01.toc is non existent
4764 INFO: Building Tree Tree-01.toc
4797 WARNING: Tcl modules directory /usr/share/tcltk/tcl8.6/../tcl8 does not exist.
4799 INFO: Loading module hook 'hook-pickle.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4800 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4801 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4802 INFO: Loading module hook 'hook-encodings.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4837 INFO: Loading module hook 'hook-heapq.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4838 INFO: Loading module hook 'hook-lib2to3.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4849 INFO: Loading module hook 'hook-difflib.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4850 INFO: Loading module hook 'hook-xml.py' from '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
4913 INFO: Looking for ctypes DLLs
4958 INFO: Analyzing run-time hooks ...
4962 INFO: Including run-time hook '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_subprocess.py'
4963 INFO: Including run-time hook '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
4965 INFO: Including run-time hook '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
4968 INFO: Including run-time hook '/home/zh/.local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
4973 INFO: Looking for dynamic libraries
5382 INFO: Looking for eggs
5382 INFO: Python library not in binary dependencies. Doing additional searching...
5417 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
5420 INFO: Warnings written to /home/zh/workSpace/python/Test1/venv/build/deviceInfo/warn-deviceInfo.txt
5439 INFO: Graph cross-reference written to /home/zh/workSpace/python/Test1/venv/build/deviceInfo/xref-deviceInfo.html
5445 INFO: checking PYZ
5445 INFO: Building PYZ because PYZ-00.toc is non existent
5445 INFO: Building PYZ (ZlibArchive) /home/zh/workSpace/python/Test1/venv/build/deviceInfo/PYZ-00.pyz
5721 INFO: Building PYZ (ZlibArchive) /home/zh/workSpace/python/Test1/venv/build/deviceInfo/PYZ-00.pyz completed successfully.
5724 INFO: checking PKG
5724 INFO: Building PKG because PKG-00.toc is non existent
5724 INFO: Building PKG (CArchive) deviceInfo.pkg
5752 INFO: Building PKG (CArchive) deviceInfo.pkg completed successfully.
5753 INFO: Bootloader /home/zh/.local/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
5753 INFO: checking EXE
5753 INFO: Building EXE because EXE-00.toc is non existent
5753 INFO: Building EXE from EXE-00.toc
5753 INFO: Copying bootloader EXE to /home/zh/workSpace/python/Test1/venv/build/deviceInfo/deviceInfo
5754 INFO: Appending PKG archive to custom ELF section in EXE
5761 INFO: Building EXE from EXE-00.toc completed successfully.
5763 INFO: checking COLLECT
5763 INFO: Building COLLECT because COLLECT-00.toc is non existent
5763 INFO: Building COLLECT COLLECT-00.toc
5787 INFO: Building COLLECT COLLECT-00.toc completed successfully.

3: 输出

执行完成后,会在同级目录下生成:

deviceInfo.spec以及dist文件夹.

deviceInfo.spec文件内容:

spec文件包含了打包的配置信息,可以通过修改spec来自定义打包行为.

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(
    ['deviceInfo.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='deviceInfo',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='deviceInfo',
)

dist文件夹内容如下:
在这里插入图片描述

4: 运行

双击运行deviceInfo.exe

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值