How to fix “ImportError: No module named setuptools” on Linux

Question: I was trying to install a Python package using setup.py ("python setup.py install"). However, it failed with the following error message:

Traceback (most recent call last):
File "setup.py", line 2, in
import setuptools
ImportError: No module named 'setuptools'

How can I fix this error?

There are several ways to package, distribute and install Python modules and programs. The most popular tools are distutils and setuptools. distutils is the standard Python packaging tool, while setuptools is a more feature-rich alternative to distutils. For example, unlike distutils, setuptools supports package dependency management, and can search for third-party modules in PyPi (Python Package Index). This makes setuptools more user-friendly.

To install a Python application packaged with setuptools, you can use the following command.

$ python (or python3) setup.py install

setup.py will then use setuptools module to retrieve and build the package as well as all dependent modules. Naturally, you must make sure that setuptools is available on your system. Without setuptools, you will encounter the error: "ImportError: No module named 'setuptools'".

To install setuptools on Debian, Ubuntu or Mint:

$ sudo apt-get install python-setuptools

For Python 3.X applications, install python3-setuptools instead.

$ sudo apt-get install python3-setuptools

On Red Hat based systems, setuptools comes pre-installed. For whatever reason if you want to install it, use the yum command.

$ sudo yum python-setuptools
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

2 thoughts on “How to fix “ImportError: No module named setuptools” on Linux

  1. I am trying to install MySQLdb by running the command "python setup.py install". But it throws an error as
    import setuptools
    ImportError: no module named setuptools

    I have checked for the setuptools and it is available in /usr/local/lib/python2.7/dist-packages/setuptools
    But when I try to reinstall it says,
    Requirement already satisfied (use –upgrade to upgrade): setuptools in /usr/local/lib/python2.7/dist-packages

    Please help me in resolving the issue.

    • Make sure that you can have read/write access to /usr/local/lib/python2.7/dist-packages/setuptools.

      Also, it's a good idea to check "sys.path" used by python (use the following command).

      $ python
      >>> import sys
      >>> print '\n'.join(sys.path)

      If the output does not contain /usr/local/lib/python2.7/dist-packages, add it to PYTHONPATH environment variable:

      export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages
      

Leave a comment

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