Modulenotfounderror: no module named ‘pkg_resources’ [Solved]

In this tutorial, we will show how to solve the Modulenotfounderror: no module named ‘pkg_resources’ error.

Also, we will explain what are the root causes of this error.

Prior to that pkg_resources is a module that is part of the setuptools library.

Wherein setuptools is a collection of enhancements to the Python distutils module that allows developers to more easily build and distribute Python packages.

Additionally, pkg_resources provides runtime support for working with installed Python packages.

What is modulenotfounderror: no module named ‘pkg_resources’?

Modulenotfounderror: no module named ‘pkg_resources’ error occurs when setuptools is not installed in your python environment.

Since the “pkg_resources” module is part of the setuptools library, which is used for packaging and distributing Python projects.

That provides functionality for managing Python package dependencies and metadata.

Therefore the error typically occurs when a Python script or application tries to import a module called “pkg_resources” but the module is not found in the Python environment.

How to solve modulenotfounderror: no module named pkg_resources

So here are the following ways you can consider to fix this error.

  1. Install or upgrade setuptools

    To install or upgrade setuptools, open your terminal and try the following command.

    pip install –upgrade setuptools

    install setuptools

    If you are using Python 3: pip3 install –upgrade setuptools

    When you don’t have pip in PATH:
    python -m pip install –upgrade setuptools or
    python3 -m pip install –upgrade setuptools

    If you are using Windows: py -m pip install –upgrade setuptools

    Note: If the error continues uninstall and install again the module.

  2. Update pip version, setuptools, and wheel

    If the error continues then update the version of your pip, setuptools, and wheel.

    To do this follow the commands below:

    pip install –upgrade pip setuptools wheel

    update pip setuptools and wheel

    If you are using Python 3 use this command:

    pip3 install –upgrade pip setuptools wheel
    python -m pip install –upgrade pip setuptools wheel
    python3 -m pip install –upgrade pip setuptools wheel


    If you are using Windows
    py -m pip install –upgrade pip setuptools wheel

  3. Create a virtual environment

    After all of the steps you have done above alternatively we can create virtual environment wherein we can install pip and setuptools automatically.

    Here are the following commands:

    1. Make sure to use the correct version of Python in creating venv
    python -m venv venv

    2. This is to activate in Unix or MacOS
    source venv/bin/activate

    3. This is to activate in Windows cmd
    venv\Scripts\activate.bat

    4. This to activate in Windows Powershell
    venv\Scripts\Activate.ps1

    5. If you don’t have one install the modules in your requirements.txt file
    pip install -r requirements.txt

Root Causes of modulenotfounderror: no module named pkg_resources

The several causes of why this modulenotfounderror: no module named pkg_resources error occurs are the following:

  • Missing or outdated setuptools
    • If you don’t have setuptools installed, or if it’s outdated, you might encounter the “No module named ‘pkg_resources'” error.
    • Additionally, setuptools is a package that provides tools for building, distributing, and installing Python packages.
  •  Broken “pkg_resources” Module
    • If the “pkg resources” module’s files are not properly integrated into the Python environment.
    • As a result, the resource files are not accessible to the installed Python environment during the installation of any package.
  • Conflicting package installations
    • Sometimes, you might have multiple installations of a package on your system, and Python might be trying to import the wrong one.
    • This can happen if you’ve installed a package using both pip and your system’s package manager, or if you have multiple versions of Python installed.

Conclusion

In conclusion, Modulenotfounderror: no module named ‘pkg_resources’ error in Python can be caused by a variety of factors, including missing or outdated setuptools, broken “pkg_resources” module, and conflicting package installations.

To fix this error, you can try installing or upgrading setuptools, recreating egg-info, or uninstalling and reinstalling conflicting packages.

We hope that this article has provided you with the information you need to fix this error and continue working with Python packages.

If you are finding solutions to some errors you’re encountering we also have Modulenotfounderror: no module named nlk.

Leave a Comment