ChatGPT解决这个技术问题 Extra ChatGPT

ModuleNotFoundError: No module named 'distutils.core'

I've recently upgraded from Ubuntu 18.04 to 19.04 which has python 3.7. But I work on many projects using Python 3.6.

Now when I try to create a virtualenv with Python 36 in PyCharm, it raises:

ModuleNotFoundError: No module named 'distutils.core'

https://i.stack.imgur.com/kKMZC.png

I can't figure out what to do.

I tried to install distutils:

milano@milano-PC:~$ sudo apt-get install python3-distutils
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-distutils is already the newest version (3.7.3-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

But as you can see I have the newest version.

Do you know what to do?


C
Cutton Eye

Python base interpreter does require some additional modules. Those are not installed with e.g. Ubuntu 18.04 as default.

To solve this we need to first find the python version you're running. If you have only installed one python version on your system (and you are sure about it) you can skip this step.

# from your project interpreter run
# your_project_python --version
$ python3 --version
Python 3.7.8

You now need to install for this precise python interpreter the distutils. So here the solution for this example would be:

sudo apt install python3.7-distutils
# sudo apt install python3-distutils  # would just update default python intrpreter

Keep in mind, that just running python from any command line might be an other version of python then you're running in your project!

If this hasn't helped, look for the following possibilities. This will bring you the binary which resolved from the alias in the command line.

$ which python
/usr/bin/python
$ ls -lach /usr/bin/python
lrwxrwxrwx 1 root root 9 Jun  8  2018 /usr/bin/python -> python2.7

original source: refer to this article

For this answer I've also merged, summarized, ordered and explained some of the content which has been provided by Neo, Andrei, Mostafa and Wolfgang.

As a side note for sorcerer's apprentice: You might be tempted to uninstall python interpreters. For proposed solution not necessary at all!! How ever, keep in mind that there is one python interpreter which your whole OS depends on. So this default one, you don't want to uninstall. If you do so, you're in a certain mess in finding your desktop taskbar and basically everything.


I also had to install setuptools and pip to get it to work: sudo apt-get install python3-pip and sudo apt-get install python3-setuptools
In my case, restart was not needed. Worked fine after apt install...
P
Phoenix

Other Cases

This happened on my python3.7 installation but not my main python3 after i upgrade my ubuntu to 20.04

Solution:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt install python3.7

A
Andrei Trunin

If I have multiple versions of python3 (etc 3.8 as main and 3.9 from ppa:deadsnakes/ppa) on ubuntu 20.04 (in my case kubuntu 20.04) and it doesn't work

sudo apt install python3-distutils

then it works for me

sudo apt install python3.9-distutils

M
Mostafa Ghadimi

Currently, I'm using ubuntu 18.04 and python 3.6.9. My problem was solved after running the following command as mentioned here:

sudo apt-get install python3-dev

More Details: Some modules in python are needed that not installed.


distutils might be a subset of the python3-dev? I would assume (untested), that the dev-Package of python contains much more data then just the distutils. pure code uncompiled libs etc. In short, distutils is more precise. Dev contains probably much more overhead.
@CuttonEye I have also tried distutils but it still doesn’t work properly. You are right!
W
Wolfgang Fahl

For me the problem was solved by specifically using python3 thus making sure python3.8 was used

python --version
Python 3.7.5

python3 --version
Python 3.8.5

This is what Andrei says. You have only installed the dist utils for one package. stackoverflow.com/a/64133784/5612605
E
Ernst Plesiutschnig

I still got error message after trying to install python3.9-distutils for python version 3.9 in pipenv. As I noticed here python3.9-distutils is in conflict with earlier versions of that package and cannot be installed on Ubuntu18.04.

I move on by using python_version = "3.6" with pipenv otherwise $ pipenv install would take the highest version of python and write it in the Pipfile and Pipfile.lock.

$ pipenv --rm #To remove the old environment
$ rm Pipfile* #Remove both Pipfiles
$ pipenv install --python 3.6

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now