Fixing Python ImportError: No module named pkg_resources

Share it to your friends

Python is a widely used scripting language that caters to a wide range of scenarios, ranging from web applications to data science.

One of Python’s strengths is the developer community, who created various code snippets and ready-made tools that can be used easily just by installing the tools and calling the tool name in the script.

The tool installation is even more simplified by a package manager called pip, which has been included since Python 2.7.9 and up. pip package manager is a cross-platform software, available for Windows, Linux, and Mac OS X.

However, sometimes a reliance on a package in pip might result in an error if the script is run on a computer that does not have the specific package installed. For example, you might encounter the following error:

Traceback (most recent call last):

File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

The error stems from the fact that the server does not have the package setuptools installed. The setuptools package is one of the crucial Python packages and is often used in a wide variety of scripts.

Fortunately, fixing the error is simple. You just need to issue a few commands to the terminal. The first command will reinstall the setuptools package and works in Windows (with pip installed), Mac, and Linux: 

pip install setuptools

Afterward, try rerunning the Python script. If it still complains about “ImportError: No module named pkg_resources”, you will need to install the setuptools with the installation script. Enter this following command in the terminal:

pip uninstall -y setuptools
wget -O - | python

After issuing those two commands, follow the instructions on the Terminal window to install the setuptools package. At the end of the installation process, you might need to restart Python or the computer.

Then, the setuptools package will be installed on the system, and the error “ImportError: No module named pkg_resources” will not appear again when you try to run the script. 

If for some reason, the setuptools package is still not installed in your Linux machine, you might need to install it through the system’s package manager by issuing the following command:

apt-get install python-setuptools (for Debian-based distros, such as Mint and Ubuntu)
yum install python-setuptools (for earlier RPM-based distros, such as CentOS 7 and earlier version of OpenSUSE)
dnf install python-setuptools (for newer RPM-based distros, such as CentOS 8) 

Leave a Comment