【问题标题】:pyinstaller ImportError: No module named pkg_resourcespyinstaller ImportError:没有名为 pkg_resources 的模块
【发布时间】:2015-09-17 20:47:43
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: python setuptools pyinstaller


    【解决方案1】:

    只需将其导入为:

    import pkg_resources.py2_warn
    

    在主文件中。

    【讨论】:

      【解决方案2】:

      当我的脚本导入模块 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 中提取内容。

      我的解决方法是手动将 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 脚本的同一目录中。

      【讨论】:

      • 我只是将这个错误pushed a fix 发送给PyInstaller's github。它没有正确检测 .eggs 中的 .py 文件。
      • 在 WebGL 构建期间,我在 Unity 引擎中遇到了类似的错误,您的回答帮助我找出了我应该在哪里拥有 pkg​​_resources 但没有。结果发现根本没有为 Python2 安装 pip(我只使用 Python3 进行开发),所以我只是安装了它(是的,我们不应该在 2020 年再依赖 Python2,但仍然有很多工具使用它)跨度>
      最近更新 更多