0

I've installed python 3.9 on Xubuntu 20.04 using apt. When I try to run pip, I get the error, No module named 'distutils.cmd'

For example, python3.9 -m pip --help gives the traceback

File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/saul/.local/lib/python3.9/site-packages/pip/__main__.py", line 29, in <module>
    from pip._internal.cli.main import main as _main
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/cli/main.py", line 9, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
    from pip._internal.cli import cmdoptions
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
    from pip._internal.cli.parser import ConfigOptionParser
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/cli/parser.py", line 12, in <module>
    from pip._internal.configuration import Configuration, ConfigurationError
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/configuration.py", line 26, in <module>
    from pip._internal.utils.logging import getLogger
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/utils/logging.py", line 27, in <module>
    from pip._internal.utils.misc import ensure_dir
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/utils/misc.py", line 39, in <module>
    from pip._internal.locations import get_major_minor_version
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/locations/__init__.py", line 14, in <module>
    from . import _distutils, _sysconfig
  File "/home/saul/.local/lib/python3.9/site-packages/pip/_internal/locations/_distutils.py", line 9, in <module>
    from distutils.cmd import Command as DistutilsCommand
ModuleNotFoundError: No module named 'distutils.cmd'

Where can I find distutils.cmd and where should I put it? I'm not at all knowledgeable about linux, just a naive user.

I found this post which indicated that there isn't a separate distutils module for python3.9, and seemed to suggest using virtual environments. When I tried

python3.9 -m venv /home/saul/.venvs/py39

however, I got the message

Error: Command '['/home/saul/.venvs/py39/bin/python3.9', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
saulspatz
  • 5,011
  • 5
  • 36
  • 47
  • Try sudo apt-get install python3-distutils. See https://github.com/pypa/get-pip/issues/124 for some detailed info. – gyan roy Mar 15 '22 at 19:54
  • @gyanroy I saw that, but Pawelz-RD's answer seems to say that that won't work. Indeed, when I try it, I just get a message that says `python3-distutils is already the newest version (3.8.10-0ubuntu1~20.04)` Perhaps I should have mentioned that python 3.8 is already installed. – saulspatz Mar 15 '22 at 20:19
  • I tried copying the distutils folder from /usr/lib/python3.8 into /usr/lib/python3.9, but that didn't work. I ran into version conflicts. If I try `sudo apt install python3.9-distutils,` it just substitutes python-3-distutils and tells me the latest version is installed. – saulspatz Mar 15 '22 at 20:35
  • see https://stackoverflow.com/questions/65644782/how-to-install-pip-for-python-3-9-on-ubuntu-20-04, if this helps – gyan roy Mar 15 '22 at 20:45
  • I installed python3.8 and then python3.9 and checked pip version. I was also getting the same disutils error. I followed the above StackOverflow link I shared. I have added an answer based on the steps I did to fix it. – gyan roy Mar 15 '22 at 21:11

2 Answers2

0
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

python3.9 get-pip.py (probably will give an error, I tried and it gave an error for me)

sudo apt install python3.9-distutils

python3.9 get-pip.py (can skip and directly check the version, if it works then good otherwise run this command and then check version. I ran this command before checking version)

And finally,

python3.9 -m pip -V (should give console as pip 22.x.x from some-path (python3.9) )

I ran these commands, it worked for me. Hope it helps you too.

gyan roy
  • 101
  • 5
0

I stumbled on this, which worked:

sudo apt install python3.9-venv
saulspatz
  • 5,011
  • 5
  • 36
  • 47