简体   繁体   English

ModuleNotFoundError:没有名为“distutils.core”的模块

[英]ModuleNotFoundError: No module named 'distutils.core'

I've recently upgraded from Ubuntu 18.04 to 19.04 which has python 3.7 .我最近从Ubuntu 18.04升级到具有python 3.719.04 But I work on many projects using Python 3.6 .但我使用Python 3.6从事许多项目。

Now when I try to create a virtualenv with Python 36 in PyCharm, it raises:现在,当我尝试在 PyCharm 中使用Python 36创建virtualenv时,它会引发:

ModuleNotFoundError: No module named 'distutils.core'

在此处输入图像描述

I can't figure out what to do.我不知道该怎么办。

I tried to install distutils:我尝试安装 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?你知道该怎么做吗?

Python base interpreter does require some additional modules. Python 基础解释器确实需要一些额外的模块。 Those are not installed with eg Ubuntu 18.04 as default.这些不是默认安装的,例如 Ubuntu 18.04。

To solve this we need to first find the python version you're running.要解决这个问题,我们需要首先找到您正在运行的 python 版本。 If you have only installed one python version on your system (and you are sure about it) you can skip this step.如果你的系统上只安装了一个 python 版本(并且你确定),你可以跳过这一步。

# 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.您现在需要为这个精确的 python 解释器安装 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!请记住,只是从任何命令行运行 python 可能是另一个版本的 python 然后你在你的项目中运行!

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.对于这个答案,我还合并、总结、排序和解释了 Neo、Andrei、Mostafa 和 Wolfgang 提供的一些内容。

As a side note for sorcerer's apprentice: You might be tempted to uninstall python interpreters.作为魔法师学徒的旁注:您可能想卸载 python 解释器。 For proposed solution not necessary at all,.对于根本不需要的建议解决方案,。 How ever, keep in mind that there is one python interpreter which your whole OS depends on.但是,请记住,您的整个操作系统都依赖于一个 python 解释器。 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.如果你这样做,你会在寻找桌面任务栏和基本上所有东西时陷入困境。

Other Cases其他案例

This happened on my python3.7 installation but not my main python3 after i upgrade my ubuntu to 20.04在我将我的 ubuntu 升级到 20.04 后,这发生在我的 python3.7 安装上,但不是我的主要 python3

Solution : 解决方案

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

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如果我在 ubuntu 20.04(在我的例子中是 kubuntu 20.04)上有多个版本的 python3(等 3.8 作为主要版本和来自 ppa:deadsnakes/ppa 的 3.9)并且它不起作用

sudo apt install python3-distutils

then it works for me那么它对我有用

sudo apt install python3.9-distutils

Currently, I'm using ubuntu 18.04 and python 3.6.9 .目前,我正在使用ubuntu 18.04python 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.更多详细信息:需要未安装的 python 中的某些模块。

For me the problem was solved by specifically using python3 thus making sure python3.8 was used对我来说,问题是通过专门使用 python3 解决的,因此确保使用了 python3.8

python --version
Python 3.7.5

python3 --version
Python 3.8.5

I still got error message after trying to install python3.9-distutils for python version 3.9 in pipenv.尝试在 pipenv 中为 python 版本 3.9 安装 python3.9-distutils 后,我仍然收到错误消息。 As I noticed here python3.9-distutils is in conflict with earlier versions of that package and cannot be installed on Ubuntu18.04.正如我在这里注意到的那样,python3.9-distutils 与该软件包的早期版本冲突,无法在 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 .我继续使用python_version = "3.6"和 pipenv 否则$ pipenv install将采用最高版本的 python 并将其写入PipfilePipfile.lock

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM