Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install python-distutils for old python versions

I am running Ubuntu 20.04 with python 3.6, 3.7, and 3.8 installed.

I am trying to install some packages using pip on 3.6 and 3.7 versions using 'python3.7 -m pip install package' but, I am getting this error:

ModuleNotFoundError: No module named 'distutils.util

I already have python3-distutils and python3-distutils-extra installed but pip only works for python 3.8.

How can I make pip work for installing packages on python 3.6 and 3.7?

like image 581
newbie Avatar asked May 03 '20 00:05

newbie


People also ask

How do I install Python distutils?

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.

What is distutils package Python?

The distutils package provides support for building and installing additional modules into a Python installation. The new modules may be either 100%-pure Python, or may be extension modules written in C, or may be collections of Python packages which include modules coded in both Python and C.


3 Answers

What about things like deadsnakes or pyenv, would those help? Otherwise you might need to add the apt repositories for older Ubuntu versions (say 18.04), but I am not sure what the side effects might be. This is a question that I would rather ask on Ask Ubuntu or Super User.

like image 101
sinoroc Avatar answered Oct 19 '22 20:10

sinoroc


(from bounty description)

I want a definitive solution that does NOT involve someone telling me to install:

  • python-distutils
  • python3-distutils
  • python3-distutils-extra

I will be handing out FREE DOWNVOTES to anyone who mentions installing these.

Well, I have some bad news for you. There is no "official" way to get distutils on the Debian-provided python* packages without installing these packages, not unless you go outside of Debian's default package repositories.

This is a consequence of Debian's decision to break up various parts of Python's standard library into separate packages, on the basis of their policy to split things into runtime packages and development packages. They consider distutils to fall in the "development" part, and thus, don't distribute it as part of the standard python package.


Basically, your options are:

  • install a python*-distutils package.
    • This is what your OS maintainers recommend doing in your situation.
    • You've not explained why you'll "be handing out free downvotes" for anyone recommending this, but this is genuinely the "most correct" solution.
  • install Python, from somewhere that does not break up the standard library.
    • This would mean using a package source other than the official debian repositories.
    • The easiest source would be the deadsnakes ppa.
  • compile your own Python!
    • This isn't actually that difficult, and tools like pyenv make it easy to compile and manage multiple python versions.
  • hack around the problem!
    • You could... download the sources from CPython's repo, and place the Lib/distutils folder somewhere on the import path for pythonX.Y? This is a 100% hack though and I strongly recommend not doing this. If something bad happens, because you did this, I'm not responsible.
like image 4
pradyunsg Avatar answered Oct 19 '22 20:10

pradyunsg


I've been looking for an answer to this question for almost as long as you, and finally found a solution.

Be warned, I may have screwed up my python3.8 installation as a side-effect, and it's possible the package will be uninstalled again and break things as soon as you run apt-update.

Also, it may be possible to reduce the number of commands, but here's what I ended up doing:

Run apt list to see a list of available packages:

$ apt list -a python3-distutils
Listing... Done
python3-distutils/focal-updates,focal-updates,focal-security,focal-security,now 3.8.5-1~20.04.1 all [installed]
python3-distutils/focal,focal 3.8.2-1ubuntu1 all
python3-distutils/bionic-updates,bionic-updates 3.6.9-1~18.04 all
python3-distutils/bionic,bionic 3.6.5-3 all

Then download the "bionic" deb package, because it actually contains multiple versions of distutils, for Pythons 3.6, 3.7 & 3.8:

$ apt download python3-distutils/bionic
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.9-1~18.04 [144 kB]
Fetched 144 kB in 0s (661 kB/s)

Finally, install the deb package:

$ sudo dpkg --install python3-distutils_3.6.9-1~18.04_all.deb 
dpkg: warning: downgrading python3-distutils from 3.8.5-1~20.04.1 to 3.6.9-1~18.04
(Reading database ... 193375 files and directories currently installed.)
Preparing to unpack python3-distutils_3.6.9-1~18.04_all.deb ...
Unpacking python3-distutils (3.6.9-1~18.04) over (3.8.5-1~20.04.1) ...
Setting up python3-distutils (3.6.9-1~18.04) ...

At this point, I was finally able to use pip with python3.6.


Note-1: The printouts of apt list -a above depend on whether you run an Ubuntu or a Debian distribution, and which apt-repos you have activated in /etc/apt/sources.list*.

Note-2: As seen in the case of python3-distutils/bionic, above, the name of the package does not always coincide with its contents. To view them, you have to download it and inspect it with dpkg -C <pafkage-file>, or from remote with apt-file list python3-distutils.

Note-3: In case you cannot find the exact distutils version for a specific Python release, you may install an earlier version and symlink to it. For example in Debian-unstable("sid") and "bullseye" as of January 2020, the python3-distutils package contained in their apt-repos has files just for Python3.9, and the previous apt-repos from "buster" contain files for Python3.7 only. So if you wish to install distutils for Python3.8, you have to download the "buster" package and execute:

$ sudo dpkg --unpack python3-distutils_3.7.3-1_all.deb 
$ sudo rm /usr/lib/python3.8/distutils/distutils/
$ sudo ln -w /usr/lib/python3.{7,8}/distutils/distutils/
like image 1
Dhi Avatar answered Oct 19 '22 20:10

Dhi