pyinstaller ImportError:没有名为pkg_resources的模块

时间:2015-09-17 20:47:43

标签: python setuptools pyinstaller

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

我知道“pkg_resources”指的是setuptools,但我检查过,我在site-packages下有setuptools 18.2,我看到在scripts目录下安装简单。我正在运行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

执行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

2 个答案:

答案 0 :(得分:2)

只需将其导入为:

import pkg_resources.py2_warn

在主文件中。

答案 1 :(得分: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

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

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

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