pyinstaller 打包exe运行错误 No module named ‘pkg_resources.py2_warn’
背景:
使用pyinstaller打包好exe后,运行出现以下错误。在更新pip的python库前打包是可以正常运行的。
打包过程没有出现任何错误提示。环境python3.7.4 win64
Traceback (most recent call last):
File “site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py”, line 13, in
File “c:\program files\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
ModuleNotFoundError: No module named ‘pkg_resources.py2_warn’
[3844] Failed to execute script pyi_rth_pkgres
解决方法:
我的方法是:找到 \Python37\Lib\site-packages\pkg_resources_init_.py,把86行 注释掉。重新打包即可。
import(‘pkg_resources.extern.packaging.version’)
import(‘pkg_resources.extern.packaging.specifiers’)
import(‘pkg_resources.extern.packaging.requirements’)
import(‘pkg_resources.extern.packaging.markers’)
#import(‘pkg_resources.py2_warn’)
metaclass = type
详细说明:
出现这种错误主要升级了pip里的python库导致的。所以按照网上说明,降级pip对应库即可。然而要配对py版本和库的版本很麻烦。而且牵一发而动全身。
我根据错误提示说明找到了该__init__.py文件。86行的意思是导入py2_warn。于是看了下该目录py2_warn.py文件.
#py2_warn.py
import sys
import warnings
import textwrap
msg = textwrap.dedent("""
You are running Setuptools on Python 2, which is no longer
supported and
>>> SETUPTOOLS WILL STOP WORKING <<<
in a subsequent release (no sooner than 2020-04-20).
Please ensure you are installing
Setuptools using pip 9.x or later or pin to setuptools<45
in your environment.
If you have done those things and are still encountering
this message, please comment in
https://github.com/pypa/setuptools/issues/1458
about the steps that led to this unsupported combination.
“”")
pre = “Setuptools will stop working on Python 2\n”
sys.version_info < (3,) and warnings.warn(pre + “" * 60 + msg + "” * 60)
逻辑大概是“python版本小于3,则提示警告“。这逻辑对运行根本没影响。注释掉在__init__.py的引用即可。
其实可以看下官网,按照作者所说,“【翻译】(这库)放弃了python2的支持,(调用)需要python3.5或以上的版本”。
至于为什么我明明python3.7还因导入这玩意儿报错就不知道了。大概pyinstaller为了兼容性干了什么东西把。