python用pyinstaller打包不能运行,提示 ModuleNotFoundError: No module named 'pkg_resources.py2_warn' 的最终解决办法,有用

一开始我连最基本的程序比如

print(‘hello’)

打包后都不能让正常运行,在warn***.txt文件中提示一大堆模块没有找到。
运行之后提示:
ModuleNotFoundError: No module named ‘pkg_resources.py2_warn’
[17404] Failed to execute script pyi_rth_pkgres

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module>
  File "c:\users\liu\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pkg_resources\__init__.py", line 86, in <module>
ModuleNotFoundError: No module named 'pkg_resources.py2_warn'
[17404] Failed to execute script pyi_rth_pkgres

在网上找了好多方法都不行,最后看见了这一篇文章原理及解决办法
最终的办法是修改这个路径下的文件

C:\Users\***\AppData\Local\Programs\Python\Python37\Lib\site-packages\pkg_resources\ init.py

打开之后注释第86行,保存之后,重新打包即可。

82 import(‘pkg_resources.extern.packaging.version’)
83 import(‘pkg_resources.extern.packaging.specifiers’)
84 import(‘pkg_resources.extern.packaging.requirements’)
85 import(‘pkg_resources.extern.packaging.markers’)
86 # import(‘pkg_resources.py2_warn’)
metaclass = type

具体的原因可以参见:https://blog.csdn.net/fred110119/article/details/104667749

搞了好长时间终于好了!!!

发布了5 篇原创文章 · 获赞 8 · 访问量 1677

猜你喜欢

转载自blog.csdn.net/qq_40608730/article/details/104864943