网上找了无数打包问题和exe问题帖子,都没有相似问题,都解决不了。
最后,还是静下心来,对Python打包过程摸清以后,把问题解决了。
原因是 dll动态链接库 没有被正确打包进入exe单文件内,而引发的问题。
感觉python的打包简直low到爆,完全没有Java的优雅和体面,也没有C#的便利和兼容性
python 所谓的语法简单优点也是建立在可读性差,各种变量类关系混乱,不适合大型项目
唯一真正优点可以粘合各种扩展库和工具,但是兼容性又经常出问题,往往不同工具和引用包之间频繁升级没有向下兼容的情况确实很令人头痛,而且还速度慢。
好了,吐槽完毕。
接下来描述问题,事情经过是这样滴...
做了一个带可视化UI界面,用来读取Excel文件对身份证列表,进行Table展示,并进行验证的windows独立exe程序。
开发环境运行没有任何问题,集成Anaconda和python3.11的PyCharm2024.1.4环境中
但在使用了PyCharm中External Tools 外部工具来运行pyinstall 的方式,打包程序后运行报错。
ImportError: DLL load failed while importing _ctypes: 找不到指定的模块。
[PYI-14340:ERROR] Failed to execute script 'pyi_rth_pkgres' due to unhandled exception!
运行exe时完整错误信息如下:
D:\PycharmProjects\vfIdCard\pythonProject\dist>vfIdCard_main.exe
Traceback (most recent call last):
File "PyInstaller\hooks\rthooks\pyi_rth_pkgres.py", line 170, in <module>
File "PyInstaller\hooks\rthooks\pyi_rth_pkgres.py", line 37, in _pyi_rthook
File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
File "pkg_resources\__init__.py", line 105, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
File "platformdirs\__init__.py", line 46, in <module>
File "platformdirs\__init__.py", line 26, in _set_platform_dir_class
File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
File "platformdirs\windows.py", line 4, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 378, in exec_module
File "ctypes\__init__.py", line 8, in <module>
ImportError: DLL load failed while importing _ctypes: 找不到指定的模块。
[PYI-14340:ERROR] Failed to execute script 'pyi_rth_pkgres' due to unhandled exception!
查看打包信息,发现有警告信息,部分包没有被正确找到打包进exe
1825 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
1825 INFO: Python: 3.11.9 (conda)
1850 INFO: Platform: Windows-10-10.0.19045-SP0
1850 INFO: Python environment: C:\Users\Administrator\.conda\envs\pythonProject
1851 INFO: wrote D:\PycharmProjects\vfIdCard\pythonProject\vfIdCard_main.spec
1856 INFO: Module search paths (PYTHONPATH):
['C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Scripts\\pyinstaller.exe',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\python311.zip',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\win32',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\win32\\lib',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\Pythonwin',
'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\setuptools\\_vendor',
'D:\\PycharmProjects\\vfIdCard\\pythonProject',
'C:\\ProgramData\\anaconda3\\Library\\bin']
2422 INFO: checking Analysis
2528 INFO: Building because pathex changed
2528 INFO: Running Analysis Analysis-00.toc
2528 INFO: Target bytecode optimization level: 0
2529 INFO: Initializing module dependency graph...
2529 INFO: Caching module graph hooks...
2566 INFO: Analyzing base_library.zip ...
7714 INFO: Processing standard module hook 'hook-heapq.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
7935 INFO: Processing standard module hook 'hook-encodings.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
12806 INFO: Processing standard module hook 'hook-pickle.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
16127 INFO: Caching module dependency graph...
16470 INFO: Looking for Python shared library...
16487 INFO: Using Python shared library: C:\Users\Administrator\.conda\envs\pythonProject\python311.dll
16487 INFO: Analyzing D:\PycharmProjects\vfIdCard\pythonProject\vfIdCard_main.py
16498 INFO: Processing standard module hook 'hook-PyQt6.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
16791 INFO: Processing standard module hook 'hook-PyQt6.QtWidgets.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
17087 INFO: Processing standard module hook 'hook-PyQt6.QtCore.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
18190 INFO: Processing standard module hook 'hook-PyQt6.QtGui.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
18781 INFO: Processing standard module hook 'hook-openpyxl.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
19116 INFO: Processing standard module hook 'hook-numpy.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\numpy\\_pyinstaller'
19272 WARNING: Conda distribution 'numpy', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
19959 INFO: Processing standard module hook 'hook-platform.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
20387 INFO: Processing standard module hook 'hook-sysconfig.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
22683 INFO: Processing standard module hook 'hook-difflib.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
23180 INFO: Processing standard module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
23367 INFO: Processing standard module hook 'hook-xml.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
25312 INFO: Processing standard module hook 'hook-psutil.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
28462 INFO: Processing standard module hook 'hook-charset_normalizer.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
29397 INFO: Processing standard module hook 'hook-lxml.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
31204 INFO: Processing standard module hook 'hook-lxml.etree.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
31223 INFO: Processing standard module hook 'hook-xml.etree.cElementTree.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
31424 INFO: Processing standard module hook 'hook-xml.dom.domreg.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
32974 INFO: Processing standard module hook 'hook-PIL.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
33180 INFO: Processing standard module hook 'hook-PIL.Image.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
34866 INFO: Processing pre-safe-import-module hook 'hook-typing_extensions.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
34869 INFO: SetuptoolsInfo: initializing cached setuptools info...
45364 INFO: Processing standard module hook 'hook-pycparser.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
46301 INFO: Processing standard module hook 'hook-setuptools.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
46318 INFO: Processing pre-safe-import-module hook 'hook-distutils.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
46320 INFO: Processing pre-find-module-path hook 'hook-distutils.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path'
47081 INFO: Processing standard module hook 'hook-distutils.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
47220 INFO: Processing standard module hook 'hook-distutils.util.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
47596 INFO: Processing pre-safe-import-module hook 'hook-packaging.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47599 INFO: Processing standard module hook 'hook-packaging.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
47605 INFO: Processing pre-safe-import-module hook 'hook-jaraco.text.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47606 INFO: Setuptools: 'jaraco.text' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.jaraco.text'!
47628 INFO: Processing standard module hook 'hook-setuptools._vendor.jaraco.text.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
47632 INFO: Processing pre-safe-import-module hook 'hook-importlib_resources.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47632 INFO: Setuptools: 'importlib_resources' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.importlib_resources'!
47685 INFO: Processing pre-safe-import-module hook 'hook-zipp.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47686 INFO: Setuptools: 'zipp' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.zipp'!
47737 INFO: Processing pre-safe-import-module hook 'hook-jaraco.functools.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47737 INFO: Setuptools: 'jaraco.functools' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.jaraco.functools'!
47819 INFO: Processing pre-safe-import-module hook 'hook-more_itertools.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
47821 INFO: Setuptools: 'more_itertools' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.more_itertools'!
48052 INFO: Processing pre-safe-import-module hook 'hook-jaraco.context.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
48053 INFO: Setuptools: 'jaraco.context' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.jaraco.context'!
48071 INFO: Processing pre-safe-import-module hook 'hook-backports.tarfile.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
48072 INFO: Setuptools: 'backports.tarfile' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.backports.tarfile'!
48256 INFO: Processing standard module hook 'hook-backports.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
48542 INFO: Processing pre-safe-import-module hook 'hook-importlib_metadata.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
48543 INFO: Setuptools: 'importlib_metadata' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.importlib_metadata'!
48593 INFO: Processing standard module hook 'hook-setuptools._vendor.importlib_metadata.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
48845 INFO: Processing pre-safe-import-module hook 'hook-ordered_set.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
48846 INFO: Setuptools: 'ordered_set' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.ordered_set'!
49149 INFO: Processing pre-safe-import-module hook 'hook-tomli.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
49149 INFO: Setuptools: 'tomli' appears to be a setuptools-vendored copy - creating alias to 'setuptools._vendor.tomli'!
50289 INFO: Processing standard module hook 'hook-pkg_resources.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
52071 INFO: Processing pre-safe-import-module hook 'hook-platformdirs.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
52089 INFO: Processing standard module hook 'hook-platformdirs.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
52288 INFO: Processing pre-safe-import-module hook 'hook-wheel.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module'
54132 INFO: Processing standard module hook 'hook-PIL.ImageFilter.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
55162 INFO: Processing module hooks (post-graph stage)...
58002 INFO: Processing standard module hook 'hook-lxml.isoschematron.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
58602 INFO: Processing standard module hook 'hook-PIL.SpiderImagePlugin.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
60453 INFO: Processing standard module hook 'hook-lxml.objectify.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\_pyinstaller_hooks_contrib\\stdhooks'
60473 INFO: Performing binary vs. data reclassification (128 entries)
60567 INFO: Looking for ctypes DLLs
60770 INFO: Analyzing run-time hooks ...
60781 INFO: Including run-time hook 'pyi_rth_inspect.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60785 INFO: Including run-time hook 'pyi_rth_pkgres.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60795 INFO: Including run-time hook 'pyi_rth_setuptools.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60798 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60802 INFO: Including run-time hook 'pyi_rth_pkgutil.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60805 INFO: Including run-time hook 'pyi_rth_pyqt6.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks'
60809 INFO: Processing pre-find-module-path hook 'hook-_pyi_rth_utils.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path'
60813 INFO: Processing standard module hook 'hook-_pyi_rth_utils.py' from 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyInstaller\\hooks'
60856 INFO: Looking for dynamic libraries
63297 INFO: Extra DLL search directories (AddDllDirectory): ['C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyQt6\\Qt6\\bin', 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\numpy.libs']
63297 INFO: Extra DLL search directories (PATH): ['C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyQt6\\Qt6\\bin']
65998 WARNING: Library not found: could not resolve 'libcrypto-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_hashlib.pyd'.
65999 WARNING: Library not found: could not resolve 'liblzma.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_lzma.pyd'.
65999 WARNING: Library not found: could not resolve 'LIBBZ2.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_bz2.pyd'.
66000 WARNING: Library not found: could not resolve 'libssl-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ssl.pyd'.
66000 WARNING: Library not found: could not resolve 'libcrypto-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ssl.pyd'.
66000 WARNING: Library not found: could not resolve 'ffi.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ctypes.pyd'.
66001 WARNING: Library not found: could not resolve 'yaml.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\yaml\\_yaml.cp311-win_amd64.pyd'.
66063 INFO: Warnings written to D:\PycharmProjects\vfIdCard\pythonProject\build\vfIdCard_main\warn-vfIdCard_main.txt
66351 INFO: Graph cross-reference written to D:\PycharmProjects\vfIdCard\pythonProject\build\vfIdCard_main\xref-vfIdCard_main.html
66459 INFO: checking PYZ
66605 INFO: checking PKG
66654 INFO: Building because toc changed
66655 INFO: Building PKG (CArchive) vfIdCard_main.pkg
87363 INFO: Building PKG (CArchive) vfIdCard_main.pkg completed successfully.
87379 INFO: Bootloader C:\Users\Administrator\.conda\envs\pythonProject\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
87379 INFO: checking EXE
87404 INFO: Rebuilding EXE-00.toc because vfIdCard_main.exe missing
87404 INFO: Building EXE from EXE-00.toc
87405 INFO: Copying bootloader EXE to D:\PycharmProjects\vfIdCard\pythonProject\dist\vfIdCard_main.exe
87413 INFO: Copying icon to EXE
87419 INFO: Copying 0 resources to EXE
87419 INFO: Embedding manifest in EXE
87423 INFO: Appending PKG archive to EXE
87509 INFO: Fixing EXE headers
88633 INFO: Building EXE from EXE-00.toc completed successfully.
Process finished with exit code 0
其中关键信息 如下内容:
63297 INFO: Extra DLL search directories (PATH): ['C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\PyQt6\\Qt6\\bin']
65998 WARNING: Library not found: could not resolve 'libcrypto-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_hashlib.pyd'.
65999 WARNING: Library not found: could not resolve 'liblzma.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_lzma.pyd'.
65999 WARNING: Library not found: could not resolve 'LIBBZ2.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_bz2.pyd'.
66000 WARNING: Library not found: could not resolve 'libssl-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ssl.pyd'.
66000 WARNING: Library not found: could not resolve 'libcrypto-3-x64.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ssl.pyd'.
66000 WARNING: Library not found: could not resolve 'ffi.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\DLLs\\_ctypes.pyd'.
66001 WARNING: Library not found: could not resolve 'yaml.dll', dependency of 'C:\\Users\\Administrator\\.conda\\envs\\pythonProject\\Lib\\site-packages\\yaml\\_yaml.cp311-win_amd64.pyd'.
在打包PyQt6 之后,可能是Pyqt间接引用的库没有被打包进去(并非我自己程序中直接引用),python的打包程序实在有点呆,这点不得不承认ms家的东西起码这点是做到位。
这些相关的dll 都在C:\ProgramData\anaconda3\Library\bin 目录下都存在
然后把路径加到打包命令中去
pyinstaller -D -F -p "C:\ProgramData\anaconda3\Library\bin\" -F $FileNameWithoutExtension$.py
结果报错依旧,网上找了一圈,基本就找不到解决相关问题的文章,近似的解决方法比如升级打包程序,都没有用。
最后,把相关的dll复制到 打包目的目录dist中,exe文件可以正常执行了。
但是我有强迫症,本来单独一个exe 可执行文件简单干净,分发给其他人使用也很便利,凭空多出来怎么多罗里吧嗦的dll库文件,心里实在难受不过。
最后阅读了详细的打包文档,找到了 打包中的 --add-data 参数,可以强制把指定路径下的各类文件打包进exe中。
PyCharm中External Tools 外部工具中这样写:
-D -F --add-data "D:\PycharmProjects\vfIdCard\lib\*.dll:bin" $FileNameWithoutExtension$.py
直接打包这样写:
pyinstaller -w -F --add-data "D:\PycharmProjects\vfIdCard\lib\*.dll:bin" vfIdCard_main.py
上面的D:\PycharmProjects\vfIdCard\lib\ 是我把需要的库拷到自己目录后打包
你们也可以直接指定anaconda3的路径 C:\ProgramData\anaconda3\Library\bin\*.dll
但是不推荐这样,因为目录下不相关的dll太多了,打出来的包会很大,又会触发强迫症的。
路径后面的 :bin 意思是把文件打包放入的目标目录,不用担心,指定单文件打包后,exe的同级目录中不会有这个bin目录。
那这个目标目录名写了这个不是没用吗?为什么要写?
因为,不写这个目标目录,打包时会报错,要求你一定加上。
这不是bug,而是.exe单文件执行时,会偷偷的把exe文件内的dll库和相关的各种资源文件复制到windows 盘的临时文件目录
双击.exe文件后,启动你写的python程序之前,系统会根据你写的打包参数中--add-data的信息来行动。
具体过程:
首先悄悄在 C:\Users\Administrator\AppData\Local\Temp 下生成一个大小写混写的随机字母的临时目录(这个临时目录名称每次都是随机变化的),比如_ME150482, _ME固定,后面数字随机。
然后根据--add-data 参数后面描述的目标目录命:bin ,在临时目录下创建bin目录(这个bin是可以自己随便定义的)。
创建以后把所有--add-data 中指定的dll和各种资源文件都从exe文件中解压复制到bin目录中,再开始启动你写的python程序。
等你的python exe程序执行完毕关闭窗口以后,会有少量停顿,exe正在把dll所在的临时目录删除掉,等下一次执行又会重新创建和复制dll过去。
所以,打包时候尽量少一些无用的dll和扩展文件,可以快exe的启动速度和关闭速度。
单文件exe容量太大话,毕竟要有一个先解压缩文件和复制文件的过程,特别是exe文件在其它机械硬盘中,而windows系统安装在c盘,需要跨盘解压缩复制文件,更加会拖慢运行时间,python解释器已经够慢了,在肥一点慢上加慢,时间不要钱吗?生命不要钱吗?
好了,今天,就到这里,就到这里吧,休息去吧。