python程序打包

python程序打包

1.安装环境

sudo pip install pyinstaller

2.打包程序

pyinstaller -F show_pkl.py

(py38) randy@Sesame:~/san_jie_ji_yuan$ pyinstaller -F show_pkl.py 
130 INFO: PyInstaller: 5.9.0
130 INFO: Python: 3.8.16 (conda)
143 INFO: Platform: Linux-5.15.0-71-generic-x86_64-with-glibc2.17
143 INFO: wrote /home/qiancj/san_jie_ji_yuan/show_pkl.spec
145 INFO: UPX is not available.
145 INFO: Extending PYTHONPATH with paths
['/home/qiancj/san_jie_ji_yuan']
319 INFO: checking Analysis
319 INFO: Building because hookspath changed
319 INFO: Initializing module dependency graph...
320 INFO: Caching module graph hooks...
324 INFO: Analyzing base_library.zip ...
828 INFO: Loading module hook 'hook-encodings.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
1060 INFO: Loading module hook 'hook-heapq.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
1536 INFO: Loading module hook 'hook-pickle.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
2074 INFO: Caching module dependency graph...
2132 INFO: running Analysis Analysis-00.toc
2138 INFO: Analyzing /home/qiancj/san_jie_ji_yuan/show_pkl.py
2140 INFO: Processing module hooks...
2146 INFO: Looking for ctypes DLLs
2147 INFO: Analyzing run-time hooks ...
2150 INFO: Looking for dynamic libraries
2829 INFO: Looking for eggs
2829 INFO: Python library not among binary dependencies. Performing additional search...
2835 INFO: Using Python library /home/qiancj/anaconda3/envs/py38/lib/libpython3.8.so.1.0
2836 INFO: Warnings written to /home/qiancj/san_jie_ji_yuan/build/show_pkl/warn-show_pkl.txt
2844 INFO: Graph cross-reference written to /home/qiancj/san_jie_ji_yuan/build/show_pkl/xref-show_pkl.html
2847 INFO: checking PYZ
2848 INFO: checking PKG
2848 INFO: Building because toc changed
2848 INFO: Building PKG (CArchive) show_pkl.pkg
6395 INFO: Building PKG (CArchive) show_pkl.pkg completed successfully.
6396 INFO: Bootloader /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
6396 INFO: checking EXE
6396 INFO: Building because toc changed
6396 INFO: Building EXE from EXE-00.toc
6397 INFO: Copying bootloader EXE to /home/qiancj/san_jie_ji_yuan/dist/show_pkl
6397 INFO: Appending PKG archive to custom ELF section in EXE
6410 INFO: Building EXE from EXE-00.toc completed successfully.

生成的可执行程序路径:就在上面倒数第3行6397 INFO: Copying bootloader EXE to /home/qiancj/san_jie_ji_yuan/dist/show_pkl

问题

上述程序打包好了之后运行会报错

(py38) qiancj@qiancj-HP-ZBook-G8:~/codes/scripts/Python/dist$ ./show_pkl 
Traceback (most recent call last):
  File "show_pkl.py", line 87, in <module>
  File "show_pkl.py", line 81, in main
  File "show_pkl.py", line 15, in read_pickle
ModuleNotFoundError: No module named 'numpy'
[17122] Failed to execute script 'show_pkl' due to unhandled exception!

原因无非2种:

  • 当前python环境中没有 numpy 库
  • pyinstaller打包时未带上numpy库
  1. 可以查看当前路径下numpy是否安装
(py38) qiancj@qiancj-HP-ZBook-G8:~/codes/scripts/Python/dist$ pip show numpy
Name: numpy
Version: 1.24.3
Summary: Fundamental package for array computing in Python
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: 
License: BSD-3-Clause
Location: /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages
Requires: 
Required-by: scipy

环境中安装了numpy,那就是pyinstaller打包的问题

  1. 重新打包
pyinstaller -F -p /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/ show_pkl.py --hidden-import numpy

其中:

  • -F:生成单一的可执行文件,将库文件包含在可执行文件中

  • -p:提供库/包的位置,以英文的分号(;)间隔

  • -i :制定可执行文件的图标

  • –hidden-import:解决命名中不可见的导入,可多次使用

    例如:pyinstaller -F --hidden-import matplotlib.backends.backend_tkagg --hidden-import numpy

(py38) randy@Sesame:~/san_jie_ji_yuan$ pyinstaller -F -p /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/ show_pkl.py --hidden-import numpy 
129 INFO: PyInstaller: 5.9.0
129 INFO: Python: 3.8.16 (conda)
141 INFO: Platform: Linux-5.15.0-71-generic-x86_64-with-glibc2.17
141 INFO: wrote /home/qiancj/san_jie_ji_yuan/show_pkl.spec
143 INFO: UPX is not available.
144 INFO: Extending PYTHONPATH with paths
['/home/qiancj/san_jie_ji_yuan',
 '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages']
319 INFO: checking Analysis
320 INFO: Building because pathex changed
320 INFO: Initializing module dependency graph...
320 INFO: Caching module graph hooks...
324 INFO: Analyzing base_library.zip ...
928 INFO: Loading module hook 'hook-heapq.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
965 INFO: Loading module hook 'hook-encodings.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
1536 INFO: Loading module hook 'hook-pickle.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
2057 INFO: Caching module dependency graph...
2117 INFO: running Analysis Analysis-00.toc
2123 INFO: Analyzing /home/qiancj/san_jie_ji_yuan/show_pkl.py
2125 INFO: Analyzing hidden import 'numpy'
2138 INFO: Loading module hook 'hook-numpy.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/numpy/_pyinstaller'...
2146 WARNING: Conda distribution 'numpy', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
2596 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
2725 INFO: Loading module hook 'hook-xml.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
3002 INFO: Loading module hook 'hook-difflib.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
3072 INFO: Loading module hook 'hook-platform.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
3189 INFO: Loading module hook 'hook-sysconfig.py' from '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks'...
3665 INFO: Processing module hooks...
3666 WARNING: Hidden import "six" not found!
3706 INFO: Looking for ctypes DLLs
3709 INFO: Analyzing run-time hooks ...
3711 INFO: Including run-time hook '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
3712 INFO: Including run-time hook '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
3713 INFO: Including run-time hook '/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
3718 INFO: Looking for dynamic libraries
/home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/building/build_main.py:177: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.
  __import__(package)
1107 WARNING: Cannot find libgfortran-040039e1.so.5.0.0 (needed by /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/numpy/core/../../numpy.libs/libopenblas64_p-r0-15028c96.3.21.so)
1121 WARNING: Cannot find libquadmath-96973f99.so.0.0.0 (needed by /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/numpy/core/../../numpy.libs/libgfortran-040039e1.so.5.0.0)
4959 INFO: Looking for eggs
4959 INFO: Python library not among binary dependencies. Performing additional search...
4976 INFO: Using Python library /home/qiancj/anaconda3/envs/py38/lib/libpython3.8.so.1.0
4990 INFO: Warnings written to /home/qiancj/san_jie_ji_yuan/build/show_pkl/warn-show_pkl.txt
5008 INFO: Graph cross-reference written to /home/qiancj/san_jie_ji_yuan/build/show_pkl/xref-show_pkl.html
5013 INFO: checking PYZ
5013 INFO: Building because toc changed
5013 INFO: Building PYZ (ZlibArchive) /home/qiancj/san_jie_ji_yuan/build/show_pkl/PYZ-00.pyz
5284 INFO: Building PYZ (ZlibArchive) /home/qiancj/san_jie_ji_yuan/build/show_pkl/PYZ-00.pyz completed successfully.
5287 INFO: checking PKG
5288 INFO: Building because toc changed
5288 INFO: Building PKG (CArchive) show_pkl.pkg
12821 INFO: Building PKG (CArchive) show_pkl.pkg completed successfully.
12823 INFO: Bootloader /home/qiancj/anaconda3/envs/py38/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
12823 INFO: checking EXE
12823 INFO: Building because toc changed
12823 INFO: Building EXE from EXE-00.toc
12824 INFO: Copying bootloader EXE to /home/qiancj/san_jie_ji_yuan/dist/show_pkl
12824 INFO: Appending PKG archive to custom ELF section in EXE
12852 INFO: Building EXE from EXE-00.toc completed successfully.

pyinstaller 常用参数含义

  --distpath DIR        Where to put the bundled app (default: ./dist)
   --workpath WORKPATH   Where to put all the temporary work files, .log, .pyz and etc. (default: ./build)
    --clean               Clean PyInstaller cache and remove temporary files before building.
  --log-level LEVEL     Amount of detail in build-time console messages. LEVEL may be one of TRACE, DEBUG, INFO, WARN, DEPRECATION, ERROR, FATAL (default:
                        INFO). Also settable via and overrides the PYI_LOG_LEVEL environment variable.

 -v, --version         Show program version info and exit.
 -D, --onedir          Create a one-folder bundle containing an executable (default)
  -F, --onefile         Create a one-file bundled executable.
  -p DIR, --paths DIR   A path to search for imports (like using PYTHONPATH). Multiple paths are allowed, separated by ``':'``, or use this option multiple
                        times. Equivalent to supplying the ``pathex`` argument in the spec file.
  --hidden-import MODULENAME, --hiddenimport MODULENAME
                        Name an import not visible in the code of the script(s). This option can be used multiple times.
                       
  --splash IMAGE_FILE   (EXPERIMENTAL) Add an splash screen with the image IMAGE_FILE to the application. The splash screen can display progress updates
                        while unpacking.
  -c, --console, --nowindowed
                        Open a console window for standard i/o (default). On Windows this option has no effect if the first script is a '.pyw' file.
  -i <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">, --icon <FILE.ico or FILE.exe,ID or FILE.icns or Image or "NONE">
                        FILE.ico: apply the icon to a Windows executable. FILE.exe,ID: extract the icon with ID from an exe. FILE.icns: apply the icon to the
                        .app bundle on Mac OS. If an image file is entered that isn't in the platform format (ico on Windows, icns on Mac), PyInstaller tries
                        to use Pillow to translate the icon into the correct format (if Pillow is installed). Use "NONE" to not apply any icon, thereby
                        making the OS show some default (default: apply PyInstaller's icon). This option can be used multiple times.
  -r RESOURCE, --resource RESOURCE
                        Add or update a resource to a Windows executable. The RESOURCE is one to four items, FILE[,TYPE[,NAME[,LANGUAGE]]]. FILE can be a
                        data file or an exe/dll. For data files, at least TYPE and NAME must be specified. LANGUAGE defaults to 0 or may be specified as
                        wildcard * to update all resources of the given TYPE and NAME. For exe/dll files, all resources from FILE will be added/updated to
                        the final executable if TYPE, NAME and LANGUAGE are omitted or specified as wildcard *. This option can be used multiple times.


欢迎关注公众号【三戒纪元】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值