Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyinstaller ImportError: No module named pkg_resources

I am trying to package up my Python script using Pyinstaller. My script imports the third party modules Exscript and Netaddr. I get the error below when I try to run the executable generated by Pyinstaller.

I know "pkg_resources" refers to setuptools, but I checked and I have setuptools 18.2 under site-packages and I see easy install in the scripts directory. I'm running Python 2.7.

Any help with getting Pyinstaller to work is appreciated. FYI, I did search and review similar questions here and they did not apply to this particular problem.

Here is the output from running Pyinstaller(I only included the errors):

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

The output of executing the .exe generated by Pyinstaller:

 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
like image 706
J.Chan Avatar asked Sep 17 '15 20:09

J.Chan


2 Answers

Just import it as:

import pkg_resources.py2_warn

In the main file.

like image 131
Joao Campos Avatar answered Nov 02 '22 16:11

Joao Campos


When my script import a module apscheduler, I met a similar error:

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

It seems that pyinstaller can not extract the content from setuptools*.egg that you installed.

My workaround is to manually extract setuptools*.egg from python lib directory (PYTHON_INSTALL_PATH\lib\site-packages\setuptools*.egg) to the same directory of the problem script, and run pyinstaller again.

In my environment, copy D:\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg to the same directory of my py script, rename it to setuptools-18.4-py2.7.zip, and extract the content (don't create new direcotry setuptools-18.4-py2.7) to the same dir of my py script.

like image 1
Frankie Avatar answered Nov 02 '22 16:11

Frankie