翼度科技»论坛 编程开发 python 查看内容

Python pyinstaller类库使用学习总结

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
实践环境

python3 .9.13
pyinstaller-6.10.0-py3-none-manylinux2014_x86_64.whl
CentOS 7.9
win11
实践操作

生成Linux版可执行文件

安装Python
  1. # yum install -y gcc zlib* openssl-devel libffi-devel
  2. # wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz
  3. # mkdir -p /usr/local/python
  4. # mv Python-3.9.13.tgz /usr/local/python
  5. # tar -xvzf Python-3.9.13.tgz
  6. # cd python-3.9.13
  7. # ./configure --prefix=/usr/local/python/python3.9.13 --enable-shared
  8. # make && make install
复制代码
说明:
1、如果不安装libffi-devel,运行pyinstaller时会报错:ModuleNotFoundError: No module named '_ctypes',以下
  1. # pyinstaller simulator.py
  2. Traceback (most recent call last):
  3.   File "/usr/bin/pyinstaller", line 8, in <module>
  4.     sys.exit(_console_script_run())
  5.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 231, in _console_script_run
  6.     run()
  7.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 172, in run
  8.     parser = generate_parser()
  9.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/__main__.py", line 137, in generate_parser
  10.     import PyInstaller.building.build_main
  11.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 28, in <module>
  12.     from PyInstaller.building.api import COLLECT, EXE, MERGE, PYZ
  13.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/api.py", line 33, in <module>
  14.     from PyInstaller.building.splash import Splash  # argument type validation in EXE
  15.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/splash.py", line 23, in <module>
  16.     from PyInstaller.depend import bindepend
  17.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/depend/bindepend.py", line 15, in <module>
  18.     import ctypes.util
  19.   File "/usr/local/python/python3.9.13/lib/python3.9/ctypes/__init__.py", line 8, in <module>
  20.     from _ctypes import Union, Structure, Array
  21. ModuleNotFoundError: No module named '_ctypes'
复制代码
2、如果编译Python时不携带--enable-shared选项,运行pyinstaller时会报类似如下错误:
  1.   File "/usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/building/build_main.py", line 671, in assemble
  2.     raise PythonLibraryNotFoundError()
  3. PyInstaller.exceptions.PythonLibraryNotFoundError: Python library not found: libpython3.9.so.1.0, libpython3.9.so
  4.     This means your Python installation does not come with proper shared library files.
  5.     This usually happens due to missing development package, or unsuitable build parameters of the Python installation.
  6.     * On Debian/Ubuntu, you need to install Python development packages:
  7.       * apt-get install python3-dev
  8.       * apt-get install python-dev
  9.     * If you are building Python by yourself, rebuild with `--enable-shared` (or, `--enable-framework` on macOS).
复制代码
添加/usr/lib64/libpython3.9.so.1.0文件
  1. # find / -name libpython3.9.so.1.0
  2. /root/Python-3.9.13/libpython3.9.so.1.0
  3. /usr/local/python/python3.9.13/lib/libpython3.9.so.1.0
  4. # cp /usr/local/python/python3.9.13/lib/libpython3.9.so.1.0 /usr/lib64/libpython3.9.so.1.0
复制代码
说明:如果不执行该操作,运行pyinstaller时生成可执行文件时可能报类似如下错误:
  1. # pyinstaller simulator.py
  2. /usr/local/python/python3.9.13/bin/python3.9: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory
复制代码
安装 pyinstaller
  1. # pip3 install pyinstaller -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
复制代码
创建软连接,避免直接执行 pyinstaller 命令时,提示 pyinstaller: command not found
  1. # find / -name pyinstaller
  2. /usr/local/python/python3.9.13/bin/pyinstaller
  3. #
  4. # ln -fs /usr/local/python/python3.9.13/bin/pyinstaller /usr/binpyinstaller
  5. # pyinstaller -v
  6. 6.10.0
复制代码
生成可执行文件
  1. # pwd
  2. /root/au02Simulator
  3. # pyinstaller -y  simulator.py
  4. 87 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
  5. 87 INFO: Python: 3.9.13
  6. 89 INFO: Platform: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17
  7. 89 INFO: Python environment: /usr/local/python/python3.9.13
  8. 90 INFO: wrote /root/au02Simulator/simulator.spec
  9. 93 INFO: Module search paths (PYTHONPATH):
  10. ['/usr/local/python/python3.9.13/lib/python39.zip',
  11. '/usr/local/python/python3.9.13/lib/python3.9',
  12. '/usr/local/python/python3.9.13/lib/python3.9/lib-dynload',
  13. '/usr/local/python/python3.9.13/lib/python3.9/site-packages',
  14. '/root/au02Simulator']
  15. 166 INFO: checking Analysis
  16. 172 INFO: checking PYZ
  17. 181 INFO: checking PKG
  18. 181 INFO: Bootloader /usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
  19. 181 INFO: checking EXE
  20. 182 INFO: checking COLLECT
  21. 182 INFO: Building COLLECT COLLECT-00.toc
  22. 210 INFO: Building COLLECT COLLECT-00.toc completed successfully.
  23. # cd dist/
  24. # ll
  25. total 0
  26. drwxr-xr-x. 3 root root 40 Oct 24 12:46 simulator
  27. # cd simulator/
  28. # ll
  29. total 1204
  30. drwxr-xr-x. 4 root root    4096 Oct 24 12:46 _internal
  31. -rwxr-xr-x. 1 root root 1227016 Oct 24 12:46 simulator
复制代码
说明:这里的simulator.py为程序入口文件,即用python运行本程序时,位于其后的.py文件
只生成一个文件
  1. # pyinstaller -y --onefile simulator.py
  2. 74 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
  3. 75 INFO: Python: 3.9.13
  4. 76 INFO: Platform: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.17
  5. 76 INFO: Python environment: /usr/local/python/python3.9.13
  6. 77 INFO: wrote /root/au02Simulator/simulator.spec
  7. 79 INFO: Module search paths (PYTHONPATH):
  8. ['/usr/local/python/python3.9.13/lib/python39.zip',
  9. '/usr/local/python/python3.9.13/lib/python3.9',
  10. '/usr/local/python/python3.9.13/lib/python3.9/lib-dynload',
  11. '/usr/local/python/python3.9.13/lib/python3.9/site-packages',
  12. '/root/au02Simulator']
  13. 146 INFO: checking Analysis
  14. 149 INFO: checking PYZ
  15. 157 INFO: checking PKG
  16. 158 INFO: Building because toc changed
  17. 158 INFO: Building PKG (CArchive) simulator.pkg
  18. 7881 INFO: Building PKG (CArchive) simulator.pkg completed successfully.
  19. 7882 INFO: Bootloader /usr/local/python/python3.9.13/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
  20. 7882 INFO: checking EXE
  21. 7883 INFO: Rebuilding EXE-00.toc because simulator missing
  22. 7883 INFO: Building EXE from EXE-00.toc
  23. 7883 INFO: Copying bootloader EXE to /root/au02Simulator/dist/simulator
  24. 7883 INFO: Appending PKG archive to custom ELF section in EXE
  25. 7900 INFO: Building EXE from EXE-00.toc completed successfully.
  26. # cd dist/
  27. # ll
  28. total 12444
  29. -rwxr-xr-x. 1 root root 12741136 Oct 24 12:50 simulator
复制代码
注意,生成的文件是不带配置的,程序所需配置需要自己添加
生成Windows版可执行文件

安装 pyinstaller
同Linux,安装完成后,会在${PYTHON_HOME}\Scripts目录下生成pyinstaller.exe,为方便执行,将其所在路径添加到环境变量
生成可执行文件
同Linux
  1. pyinstaller [选项] program_entry_file.py
复制代码
pyinstaller常用选项


  • -y   档输出目录下的存在旧生成文件时,不提示删除确认提示,直接删除
  • -F,--onefile  打包为一个独立文件
  • --add-data ; (适用Win) 、--add-data : (适用Linux)  用于将非python文件如配置,打包进程序。

    • 是资源文件的路径,可以是相对路径或绝对路径,建议使用绝对路径。
    • 是资源文件在打包后在可执行文件中的目标目录路径。是相对于应用程序顶层目录的目标目录,如果要将文件放入应用程序顶层目录,则使用 . 作为 。注意:如果该目录路径不存在,则会自动创建
    --add-data 可重复使用,以支持单次添加多个文件的需求

例子:
假设我们有一个Python项目,其中包含一个名为config.ini的配置文件。希望在使用PyInstaller打包项目时,将这个配置文件也一起打包进去。
  1. cd /d D:\PyProjects\ZenTaoTestcaseHelper
  2. pyinstaller --add-data "D:\PyProjects\ZenTaoTestcaseHelper\config.json;." testcase_helper.py
复制代码
生成目标结果文件路径如下:
  1. D:\PyProjects\ZenTaoTestcaseHelper\dist\testcase_helper\testcase_helper.exe
  2. D:\PyProjects\ZenTaoTestcaseHelper\dist\testcase_helper\config.json
复制代码
运行可执行文件无法正确读取配置文件解决方案

实践时遇到过这样的情况:
直接使用python program_entry_file.py的方式运行程序时,可正确执行不报错,直接运行打包生成的可执行文件时,出现报错,提示相关配置文件不存在。
经过分析发现,直接运行可执行文件时,读取配置文件的路径不对,为了避免这种情况,可在代码中添加判断,设置采用可执行文件的方式运行时的配置文件读取路径:
program_entry_file.py(程序入口文件)中判断是否是运行打包生成的可执行文件,如果是,则设置环境变量,获取可执行文件所在目录
  1. sys_executable = sys.executable
  2. if not sys_executable.endswith('python.exe') and not re.findall('python\d*$', sys_executable):
  3.     os.environ['EXECUTABLE_DIR'] = os.path.dirname(sys.executable)
复制代码
其它需要读取配置文件的py文件中添加判断
假设我们期望可执行文件从位于同级目录的conf目录下的program.conf文件中读取配置
  1. config_file_path = 'conf/program.conf'
  2. if os.getenv('EXECUTABLE_DIR'):
  3.     config_file_path = os.path.join(os.environ['EXECUTABLE_DIR'], config_file_path)
复制代码
参考链接

https://pyinstaller.org/en/v6.2.0/usage.html

来源:https://www.cnblogs.com/shouke/p/18509425
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具