19

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?

Addison
  • 7,322
  • 2
  • 39
  • 55
newbie
  • 443
  • 1
  • 4
  • 14
  • What about things like [deadsnakes](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa) or [pyenv](https://github.com/pyenv/pyenv), would those help? – sinoroc May 21 '20 at 08:18

5 Answers5

8

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • I had a virtual environment that became unusable after OS upgrade to a newer version of LinuxMint. Installing python 3.6 with pyenv and then re-creating my virtual environment solved it for me – geogeo Sep 25 '21 at 00:29
4

(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.
pradyunsg
  • 18,287
  • 11
  • 43
  • 96
  • 2
    I've already installed these packages, and they do not work for **old python versions** - thus my frustration. The [bionic file list](https://packages.ubuntu.com/bionic/all/python3-distutils/filelist) contains the needed packages for python 3.6 and 3.7, but that's no good to me, since the [focal file list](https://packages.ubuntu.com/focal/all/python3-distutils/filelist) which I've installed doesn't. Is there any way to have both? – Addison May 22 '20 at 06:27
  • 2
    I've edited the question to be even more obvious about exactly what the problem is. It's very, very easy to get pip working with python3.8 on Ubuntu 20.04. It is, so far, impossible to get it working with python3.7. I challenge you to try it yourself, to better understand the issue. – Addison May 22 '20 at 06:37
  • Thanks a lot for deadsnakes ppa. I've installed on Debian Bullseye (testing) and I have py 3.7 working again (together with system py 3.8). – rysson Dec 14 '20 at 10:22
1

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/
ankostis
  • 8,579
  • 3
  • 47
  • 61
Dhi
  • 89
  • 1
  • 6
1

The instructions here worked for me: https://www.linuxcapable.com/how-to-install-python-3-7-on-ubuntu-20-04-lts/

Namely this one sudo apt install python3.7-distutils

Was trying to install checkov and getting the ModuleNotFound error. After the above, it installed just fine following these instructions

  • `sudo apt install python3.7-distutils` works for me but I changed python3.7 to python3.11 for 3.11 python currently version – Danh Le Feb 15 '23 at 11:56
0

Here is what has worked for me on Ubuntu 22.04 for Python 3.6. pip won't install as distutils is not available.

  • install python3.6 via apt
  • check the exact version with python3.6 --version
  • find a matching tag here: https://github.com/python/cpython.git, i.e. 3.6.9
  • adapt and call git clone --depth=1 -b v3.6.9 https://github.com/python/cpython.git
  • export PYTHONPATH=`pwd`/cpython/Lib
  • curl https://bootstrap.pypa.io/pip/3.6/get-pip.py | python3.6 - --user
  • python3.6 -m pip list

If the old version is not available, cd into the cpython source directory and use e.g. ./configure --enable-optimizations --prefix=$HOME/.local and make -j4 and make altinstall (set some PREFIX to install to when calling configure, so it won't clobber the system, but note that the install path has to be in your search paths)

vbar
  • 1
  • 1