Search code examples
pythonpython-3.xsetuptools

"No module named setuptools" error, despite it being installed


I am trying (and failing) to run a setup.py file to install a python package. The machine that I am trying to install this package on is running Ubuntu 16.04.6 LTS, and I'm not super familiar with linux OS, so maybe I am overlooking something.

When I run setup.py, I encounter the following error message.

  File "setup.py", line 3, in <module>
    from setuptools import setup, Extension
ImportError: No module named setuptools

However, I checked my python installation and it seems to have setuptools installed, as I can import the package properly with no errors.

(bu) kmao:~$ python
Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from setuptools import setup, Extension
>>>

Is there a reason why I can import setuptools in my python terminal, but cannot when running the setup.py script? How do I go about fixing this issue?

NB: The setup.py was called from a script setup.sh that contains the following lines:

python setup.py clean
python setup.py build_ext
python setup.py build_py -c
python setup.py bdist bdist_wheel

Solution

  • The setup.sh script is executed in a new shell environment, so it is probably using your default Python interpreter instead of the Python interpreter from your virtual Python environment. And apparently, setuptools is not installed for the default interpeter (NB: that would possibly require root access).

    If the shell script contains only the four lines you mentioned above, you could simply "source" the script by entering either

    source setup.sh
    

    or

    . setup.sh
    

    Note that this solution might not work if the script contains other code as well.

    By "sourcing" the script, all commands are executed in the same shell (i.e. without starting a new shell), so this makes sure the Python interpreter from your virtual environment is used.