pyinstaller 导入错误:没有名为 pkg_resources 的模块

J.C*_*han 6 python setuptools pyinstaller

我正在尝试使用 Pyinstaller 打包我的 Python 脚本。我的脚本导入了第三方模块 Exscript 和 Netaddr。当我尝试运行 Pyinstaller 生成的可执行文件时,出现以下错误。

我知道“pkg_resources”指的是 setuptools,但是我检查了并且我在 site-packages 下有 setuptools 18.2,我看到在脚本目录中很容易安装。我正在运行 Python 2.7。

任何有关让 Pyinstaller 工作的帮助表示赞赏。仅供参考,我确实在这里搜索并查看了类似的问题,但它们不适用于这个特定问题。

这是运行 Pyinstaller 的输出(我只包含错误):

C:\Python27\Lib\PyInstaller-2.1>pyinstaller c:\users\<username>\pycharmprojects\neworking2\network_login.py
5759 INFO: building because out00-Tree.toc missing or bad
5983 INFO: building because out01-Tree.toc missing or bad
7097 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
9569 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
9591 INFO: rebuilding out00-EXE.toc because network_login.exe missing
Run Code Online (Sandbox Code Playgroud)

执行 Pyinstaller 生成的 .exe 的输出:

 C:\Python27\Lib\PyInstaller-2.1\network_login\dist\network_login>network_loginTraceback (most recent call last):
File "build\bdist.win32\egg\paramiko\__init__.py", line 30, in <module>
File "build\bdist.win32\egg\paramiko\transport.py", line 49, in <module>
File "build\bdist.win32\egg\paramiko\dsskey.py", line 26, in <module>
File "build\bdist.win32\egg\Crypto\PublicKey\DSA.py", line 89, in <module>
File "build\bdist.win32\egg\Crypto\Random\__init__.py", line 28, in <module>
File "build\bdist.win32\egg\Crypto\Random\OSRNG\__init__.py", line 34, in <module>
File "build\bdist.win32\egg\Crypto\Random\OSRNG\nt.py", line 28, in <module>
File "build\bdist.win32\egg\Crypto\Random\OSRNG\winrandom.py", line 7, in <module>
File "build\bdist.win32\egg\Crypto\Random\OSRNG\winrandom.py", line 3, in __bootstrap__ImportError: No module named pkg_resources
Run Code Online (Sandbox Code Playgroud)

小智 1

当我的脚本导入模块 apscheduler 时,我遇到了类似的错误:

Traceback (most recent call last):
...
    File "d:\Anaconda\lib\site-packages\apscheduler\schedulers\base.py", line 9, in <module>
      from pkg_resources import iter_entry_points
ImportError: No module named pkg_resources
scheduler2 returned -1
Run Code Online (Sandbox Code Playgroud)

pyinstaller 似乎无法从您安装的 setuptools*.egg 中提取内容。

我的解决方法是手动将 setuptools*.egg 从 python lib 目录 (PYTHON_INSTALL_PATH\lib\site-packages\setuptools*.egg) 提取到问题脚本的同一目录,然后再次运行 pyinstaller。

在我的环境中,将 D:\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg 复制到我的 py 脚本的同一目录,将其重命名为 setuptools-18.4-py2.7.zip,然后解压内容(不要创建新的目录 setuptools-18.4-py2.7)到我的 py 脚本的同一目录。