Neo
Neo

Reputation: 13901

How can I install distutils for Python on Ubuntu?

I just got some space on a VPS server(running on Ubuntu 8.04), and I'm trying to install Django on it. The server has python 2.5 installed. When I run the Django installation script, I get:

amitoj@ninja:~/Django-1.2.1$ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 1, in <module>
    from distutils.core import setup
ImportError: No module named distutils.core

I found plenty of information about how to install packages using distutils; but how do I get distutils itself?


Note that Setuptools includes a modified version of distutils that includes sub-modules which were never part of the original distutils. See stdlib's distutils vs setuptools' distutils. When these sub-modules are needed, Setuptools must be installed anyway.

Also note that Setuptools is needed to get this functionality for Python 3.12 onward, because Distutils has been removed from the standard library. See Why did I get an error ModuleNotFoundError: No module named 'distutils'?. It may also be necessary within virtual environments, as in Windows: ModuleNotFoundError: No module named 'distutils'.

However, if you have an ordinary Python distribution, 3.11 or older, which came with the system, on Ubuntu, and a setup.py or other code which is looking for the standard distutils functionality - the problem is that the Ubuntu maintainers have not included it with the bundled Python. It needs to be installed using the system package manager, as described in answers here.

Upvotes: 129

Views: 422260

Answers (12)

ettanany
ettanany

Reputation: 19826

On Debian-based distros such as Ubuntu, the distutils package is mostly missing. Only the package root and distutils.version is available. The remaining code has been moved to python3-distutils, which must be installed separately.

The following command should solve the problem in Ubuntu 18.04, 20.04 and 22.04:

sudo apt-get install python3-distutils

If you're using a specific python3 minor version, different from the distro python3 default, then you may need to install the corresponding minor version of the system distutils package. E.g. for Python 3.10:

sudo apt-get install python3.10-distutils

reference: Negative Python user experience on Debian/Ubuntu - distutils is stripped down and missing most code.

Upvotes: 262

Abhinav Deshpande
Abhinav Deshpande

Reputation: 29

The distutils package provides support for building and installing additional modules into a Python installation. Python's official page for further info: here.

Now while going through the above link I learnt that setuptools is an enhanced alternative to distutils.

So try to download setuptools instead of disutils with:

pip install setuptools

and I got no more error regarding distutils not found. The program worked successfully!

Upvotes: 1

Cyebukayire
Cyebukayire

Reputation: 947

This didn't work for me: sudo apt-get install python-distutils

So, I tried this: sudo apt-get install python3-distutils

Upvotes: 1

Abdul Basit
Abdul Basit

Reputation: 1057

The module not found likely means the packages aren't installed.

Debian has decided that distutils is not a core python package, so it is not included in the last versions of debian and debian-based OSes. You should be able to do

sudo apt-get install python3-distutils
sudo apt-get install python3-apt

Upvotes: 6

Frances He
Frances He

Reputation: 112

By searching all python-distutils related package:

apt-cache search x

I get python3-distutils-extra - enhancements to the Python3 build system

Then just try:

sudo apt-get install python3-distutils-extra

Upvotes: -1

Zoltan Fedor
Zoltan Fedor

Reputation: 2107

If you are in a scenario where you are using one of the latest versions of Ubuntu (or variants like Linux Mint), one which comes with Python 3.8, then you will NOT be able to have Python3.7 distutils, alias not be able to use pip or pipenv with Python 3.7, see: How to install python-distutils for old python versions

Obviously using Python3.8 is no problem.

Upvotes: 2

Ben Amos
Ben Amos

Reputation: 1900

If you are unable to install with either of these:

sudo apt-get install python-distutils
sudo apt-get install python3-distutils

Try this instead:

sudo apt-get install python-distutils-extra

Ref: https://groups.google.com/forum/#!topic/beagleboard/RDlTq8sMxro

Upvotes: 34

Mohammad Heydari
Mohammad Heydari

Reputation: 4290

you can use sudo apt-get install python3-distutils by root permission.

i believe it worked here

Upvotes: 15

Duncan
Duncan

Reputation: 95742

The simplest way to install setuptools when it isn't already there and you can't use a package manager is to download ez_setup.py and run it with the appropriate Python interpreter. This works even if you have multiple versions of Python around: just run ez_setup.py once with each Python.

Edit: note that recent versions of Python 3 include setuptools in the distribution so you no longer need to install separately. The script mentioned here is only relevant for old versions of Python.

Upvotes: 9

mightypile
mightypile

Reputation: 8022

I ran across this error on a Beaglebone Black using the standard Angstrom distribution. It is currently running Python 2.7.3, but does not include distutils. The solution for me was to install distutils. (It required su privileges.)

    su
    opkg install python-distutils

After that installation, the previously erroring command ran fine.

    python setup.py build

Upvotes: 8

merwok
merwok

Reputation: 6905

If the system Python is borked (i.e. the OS packages split distutils in a python-devel package) and you can’t ask a sysadmin to install the missing piece, then you’ll have to install your own Python. It requires some header files and a compiler toolchain. If you can’t have those, try compiling a Python on an identical computer and just copying it.

Upvotes: -1

Manoj Govindan
Manoj Govindan

Reputation: 74795

You can install the python-distutils package. sudo apt-get install python-distutils should suffice.

Upvotes: 14

Related Questions