ubuntu pyinstaller 打包py后,运行出错,求助!

在尝试使用PyInstaller打包Python脚本hello.py和vip_movie.py时遇到错误。报错信息显示缺少_struct、pyimod02_archive和pyimod03_importers等模块。这可能是因为PyInstaller在创建可执行文件时未能正确包含某些依赖。解决方案可能包括检查Python环境的完整性,确保所有必需的库已安装,并重新运行PyInstaller打包命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错 报错 报错 报错

➜  dist pyinstaller -v
4.5.1

➜  dist whereis pyinstaller
pyinstaller: /home/maya/.local/bin/pyinstaller

➜  dist whereis python     
python: /usr/bin/python3.8 /usr/bin/python2.7-config /usr/bin/python3.8-config /usr/bin/python /usr/bin/python3.9-config /usr/bin/python2.7 /usr/bin/python3.9 /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.8 /etc/python2.7 /etc/python3.9 /usr/local/bin/python3.9-config /usr/local/bin/python3.9 /usr/local/lib/python3.8 /usr/local/lib/python2.7 /usr/local/lib/python3.9 /usr/include/python3.8 /usr/include/python2.7 /usr/include/python3.9 /usr/local/python3.9 /usr/share/python

➜  VIP_MOVIE pyinstaller -F -w -i f1.ico hello.py -p /usr/local/lib/python3.9/site-packages

94 INFO: PyInstaller: 4.5.1
94 INFO: Python: 3.9.5
98 INFO: Platform: Linux-5.11.0-36-generic-x86_64-with-glibc2.31
100 INFO: wrote /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/hello.spec
110 INFO: UPX is not available.
113 INFO: Extending PYTHONPATH with paths
['/home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE',
 '/usr/local/lib/python3.9/site-packages',
 '/home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE']
526 INFO: checking Analysis
537 INFO: Building because pathex changed
537 INFO: Initializing module dependency graph...
540 INFO: Caching module graph hooks...
552 INFO: Analyzing base_library.zip ...
6438 INFO: Processing pre-find module path hook distutils from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks/pre_find_module_path/hook-distutils.py'.
6439 INFO: distutils: retargeting to non-venv dir '/usr/lib/python3.9'
12055 INFO: Caching module dependency graph...
12435 INFO: running Analysis Analysis-00.toc
12510 INFO: Analyzing /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/hello.py
12513 INFO: Processing module hooks...
12514 INFO: Loading module hook 'hook-_tkinter.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12670 INFO: checking Tree
12682 INFO: checking Tree
12773 WARNING: Tcl modules directory /usr/share/tcltk/tcl8.6/../tcl8 does not exist.
12775 INFO: Loading module hook 'hook-heapq.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12779 INFO: Loading module hook 'hook-sysconfig.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12796 INFO: Loading module hook 'hook-multiprocessing.util.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12799 INFO: Loading module hook 'hook-pickle.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12801 INFO: Loading module hook 'hook-distutils.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12802 INFO: Loading module hook 'hook-distutils.util.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12804 INFO: Loading module hook 'hook-lib2to3.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12828 INFO: Loading module hook 'hook-encodings.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12936 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12937 INFO: Loading module hook 'hook-difflib.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
12940 INFO: Loading module hook 'hook-xml.py' from '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks'...
13094 INFO: Looking for ctypes DLLs
13171 INFO: Analyzing run-time hooks ...
13179 INFO: Including run-time hook '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py'
13183 INFO: Including run-time hook '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_multiprocessing.py'
13189 INFO: Including run-time hook '/home/maya/.local/lib/python3.9/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py'
13203 INFO: Looking for dynamic libraries
13743 INFO: Looking for eggs
13743 INFO: Python library not in binary dependencies. Doing additional searching...
13833 INFO: Using Python library /usr/local/lib/libpython3.9.so.1.0
13844 INFO: Warnings written to /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/build/hello/warn-hello.txt
13904 INFO: Graph cross-reference written to /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/build/hello/xref-hello.html
13916 INFO: checking PYZ
13920 INFO: Building because toc changed
13921 INFO: Building PYZ (ZlibArchive) /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/build/hello/PYZ-00.pyz
14728 INFO: Building PYZ (ZlibArchive) /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/build/hello/PYZ-00.pyz completed successfully.
14736 INFO: checking PKG
14737 INFO: Building because toc changed
14737 INFO: Building PKG (CArchive) PKG-00.pkg
26700 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
26706 INFO: Bootloader /home/maya/.local/lib/python3.9/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
26706 INFO: checking EXE
26708 INFO: Rebuilding EXE-00.toc because hello missing
26708 INFO: Building EXE from EXE-00.toc
26709 INFO: Appending archive to ELF section in EXE /home/maya/Documents/pythonProject/1xiangmo/VIP_MOVIE/dist/hello
26806 INFO: Building EXE from EXE-00.toc completed successfully.

➜  VIP_MOVIE ls

'字符串转 md5 工具.py'   f1.ico   f4.ico     hello.spec     vip_movie.spec
 build                   f2.ico   fa.ico     __pycache__
 dist                    f3.ico   hello.py   vip_movie.py
➜  VIP_MOVIE cd dist                    
➜  dist ls
hello  vip_movie

报错,原因是什么?怎么解决,求助!!!

➜  dist ./hello       
             
[104655] Module object for struct is NULL!
Traceback (most recent call last):
  File "struct.py", line 13, in <module>
ModuleNotFoundError: No module named '_struct'
[104655] Module object for pyimod02_archive is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod02_archive.py", line 30, in <module>
ModuleNotFoundError: No module named 'struct'
[104655] Module object for pyimod03_importers is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod03_importers.py", line 26, in <module>
ModuleNotFoundError: No module named 'pyimod02_archive'
Traceback (most recent call last):
  File "PyInstaller/loader/pyiboot01_bootstrap.py", line 17, in <module>
ModuleNotFoundError: No module named 'pyimod03_importers'
[104655] Failed to execute script 'pyiboot01_bootstrap' due to unhandled exception!

➜  dist ./vip_movie

[105048] Module object for struct is NULL!
Traceback (most recent call last):
  File "struct.py", line 13, in <module>
ModuleNotFoundError: No module named '_struct'
[105048] Module object for pyimod02_archive is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod02_archive.py", line 30, in <module>
ModuleNotFoundError: No module named 'struct'
[105048] Module object for pyimod03_importers is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod03_importers.py", line 26, in <module>
ModuleNotFoundError: No module named 'pyimod02_archive'
Traceback (most recent call last):
  File "PyInstaller/loader/pyiboot01_bootstrap.py", line 17, in <module>
ModuleNotFoundError: No module named 'pyimod03_importers'
[105048] Failed to execute script 'pyiboot01_bootstrap' due to unhandled exception!

### PyInstaller 打包 Python 文件 为了使用 PyInstaller 将 `.py` 文件打包成独立的可执行文件,需先确保 Python 和 pip 的环境变量已正确设置[^1]。 #### 安装 PyInstaller 及其依赖项 安装最新版本的 `pip` 是必要的,可以通过命令来完成: ```bash python.exe -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 接着,通过 pip 安装 PyInstaller: ```bash pip install pyinstaller ``` 对于 Windows 用户来说,还需要额外安装 `pywin32` 库以支持某些特定功能[^2]: ```bash pip install pywin32 ``` #### 基本打包过程 最简单的打包方式是在命令行输入如下指令: ```bash pyinstaller your_script.py ``` 这将会创建一个名为 `dist` 的目录,在其中可以找到生成的应用程序及其所需的库文件夹。如果希望只生成单个可执行文件,则应加上 `--onefile` 参数: ```bash pyinstaller --onefile your_script.py ``` #### 添加外部资源 当项目中有配置文件或其他静态资源时,比如 YAML 配置文件,可通过 `--add-data` 参数将其包含进来[^4]: ```bash pyinstaller --onefile --add-data "config.yaml;." your_script.py ``` 注意分号前后的路径部分取决于操作系统;Windows 上使用分号(`;`)而 Unix/Linux/MacOS 则采用冒号(`:`)作为分隔符。 #### 使用 UPX 进行压缩优化 (可选) 为了减小程序体积,可以选择集成 UPX 工具来进行压缩处理。只需定义好 `UPX_DIR` 环境变量指向 UPX 下载位置即可让 PyInstaller 自动应用压缩[^5]。 ```bash set UPX_DIR=C:\path\to\upx # 对于 Windows 用户而言 export UPX_DIR=/path/to/upx # Linux 或 MacOS 用户则这样操作 ``` 之后再次运行 PyInstaller 构建流程便会启用此特性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值