How To Fix “No module named ‘pkg_resources'” In Python

No module named 'pkg_resources' In Python

No module named ‘pkg_resources’ in Python may occur when you install a module or package your project. Follow the instructions below to find the root cause of this problem.

No module named ‘pkg_resources’ In Python

There are a lot of situations where you may see this error message:

from pkg_resources import load_entry_point
ModuleNotFoundError: No module named 'pkg_resources'

Using pip to install a package is a common scenario. But others have also had the same problem when they run other Python applications like the build system Meson.

pkg_resources And Setuptools

With that message, Python is telling you that it couldn’t find the pkg_resources module.

It provides Python libraries with APIs to allow them to access resource files. Thanks to it, frameworks and extensible applications can discover plugins automatically. The module uses the “egg” format, which is similar to “gems” in Ruby and “jars” in Java.

However, you cannot install pkg_resources as a standalone module. It comes with the Setuptools library, which includes pkg_resources and a whole host of other tools for packaging in Python. To resolve the problem, you will need to install Setuptools.

Install Setuptools

The simplest way to install Setuptools is through the package manager pip:

pip install setuptools

Most of the time, your Python installation should already have pip. This is at least the case when you download it from the official website (python.org), use a virtual environment, or use a distribution where pip hasn’t been removed.

For data analysis and software development, using virtual environments is recommended. They isolate your projects from the system-wide Python installation.

The dependencies for each project will be kept in a single place and don’t get affected by changes you make elsewhere in the system. You can have different versions of the same Python module, making it easier to maintain and collaborate with people using other platforms.

You will need to create a virtual environment first with venv:

python -m venv learnshareit

Activate the virtual environment you want to work in:

source learnshreit/bin/activate

Python will switch to the virtual environment and display its name in brackets on your console prompt. Now you can install setuptools:

(learnshareit)  $ python -m pip install setuptools

Note: you can also use pip to solve errors such as “ModuleNotFoundError: no module named ‘requests’”.

Install pip

If you don’t have pip on your system, you will need to install it first. There are various ways to do this.

With ensurepip

The built-in ensurepip module of Python can help you download and set up pip in your environment. Depending on your operation system, use these commands on your terminal app:

Linux and macOS

python -m ensurepip --upgrade

Windows

py -m ensurepip --upgrade

With get-pip.py

The get-pip.py script is another popular method for installing pip. First, download it from https://bootstrap.pypa.io/get-pip.py, then run the script:

Linux and macOS

python get-pip.py

Windows

py get-pip.py

With Linux Package Managers

If you are using a popular Linux distribution, pip and setuptools are likely available on its package repository. They are officially distributed by developers of pip and setuptools. Their versions are usually out-of-date and may take some time to catch up with the latest version in upstream.

But official repositories are a nice way to incorporate your Python installation and modules into your Linux system. You can use its package manager to install, upgrade, and uninstall Python modules just as other packages.

Follow the instructions for your distribution.

Fedora

sudo dnf install python3-pip

openSUSE

sudo zypper install python3-pip

Ubuntu/Debian

sudo apt install python3-venv python3-pip

Archlinux

sudo pacman -S python-pip

Summary

The error No module named ‘pkg_resources’ in Python occurs when you don’t have the Setuptools library in your local Python installation. Use pip and your system’s package manager to install it to make the error disappear.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *