通过idea创建python Virtualenv Environment报错“ModuleNotFoundError: No module named ‘distutils’”
一、事情经过
-
新搭建的python环境,为保证依赖隔离,通过
Add Python SDK
来创建虚拟环境 -
选择
Virtualenv Environment
,填写Location
和Base interpreter
后,点击OK
创建说明:Location为虚拟环境的存放位置,Base interpreter为该虚拟环境基于哪个解释器创建
-
然后就收到了如下报错
呵呵,不出意外的话,那应该是出意外了。开始排查
二、排查步骤
-
通过提示,似乎是缺少
distutils
模块。或许你第一时间会想到通过pip install distutils
安装distutils
,但很不幸,distutils
包不存在C:\Users\a>pip install distutils ERROR: Could not find a version that satisfies the requirement distutils (from versions: none) ERROR: No matching distribution found for distutils
-
通过提示进一步分析,我们可发现其涉及的命令包括
pip
和setuptools
C:\Users\\AppData\Roaming\JetBrains\IntelliJIdea2021.1\plugins\python\helpers\pip-20.1.1-py2.py3-none-any.whl\pip install --no-index C:\Users\a\AppData\Roaming\JetBrains\IntelliJIdea2021.1\plugins\python\helpers\setuptools-44.1.1-py2.py3-none-any.whl
通过
pip list
查看python中已安装的包,发现仅安装了pip
,而setuptools
未安装C:\Users\a>pip list Package Version ------- ------- pip 23.2.1
-
通过
pip install setuptools
安装setuptools提示:若从官方源下载比较慢,可以考虑使用国内镜像源
如阿里云:pip install setuptools -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
C:\Users\a>pip install setuptools -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple/ Collecting setuptools Downloading http://mirrors.aliyun.com/pypi/packages/55/3a/5121b58b578a598b269537e09a316ad2a94fdd561a2c6eb75cd68578cc6b/setuptools-69.0.3-py3-none-any.whl (819 kB) ---------------------------------------- 819.5/819.5 kB 4.3 MB/s eta 0:00:00 Installing collected packages: setuptools Successfully installed setuptools-69.0.3 C:\Users\a>pip list Package Version ---------- ------- pip 23.2.1 setuptools 69.0.3
安装完成,再次尝试,然而仍然报错QWQ。遇事不要慌,先冷静冷静,想想有没有什么遗漏的
-
经过小编的一番冥思苦想,注意到
inherit global site-packages
选项,我们安装的setuptools
是在Base interpreter
中的,即系统环境中的,而非虚拟环境中的,有没有可能就是漏了此处。inherit global site-packages说明:如果勾选“inherit global site-packages”,在该虚拟环境下,我们可以使用base interpreter的所有packages;反之无法调用base interpreter的packages。
大胆猜想,小心求证,勾选上再试一次。成功创建虚拟环境
三、总结
通过idea创建python Virtualenv Environment报错“ModuleNotFoundError: No module named ‘distutils’”,通过以下两个步骤解决:
-
通过
pip install setuptools
安装setuptools提示:若从官方源下载比较慢,可以考虑使用国内镜像源
如阿里云:pip install setuptools -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
-
创建“Virtualenv Environment”虚拟环境时,勾选上“inherit global site-packages”
四、心得
遇事不要慌,先冷静分析!实在不行,先发个朋友圈也行,说不定就有朋友圈大佬给你提供远程指导哦QWQ
感谢您的阅读,我们有缘再会(下次踩坑时就会又更新了)