Python环境搭建问题记录

通过idea创建python Virtualenv Environment报错“ModuleNotFoundError: No module named ‘distutils’”

一、事情经过

  1. 新搭建的python环境,为保证依赖隔离,通过Add Python SDK来创建虚拟环境

    请添加图片描述

  2. 选择Virtualenv Environment,填写LocationBase interpreter后,点击OK创建

    说明:Location为虚拟环境的存放位置,Base interpreter为该虚拟环境基于哪个解释器创建

    请添加图片描述

  3. 然后就收到了如下报错

    请添加图片描述呵呵,不出意外的话,那应该是出意外了。开始排查

二、排查步骤

  1. 通过提示,似乎是缺少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
    
  2. 通过提示进一步分析,我们可发现其涉及的命令包括pipsetuptools

    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
    
  3. 通过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。遇事不要慌,先冷静冷静,想想有没有什么遗漏的

  4. 经过小编的一番冥思苦想,注意到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’”,通过以下两个步骤解决:

  1. 通过pip install setuptools安装setuptools

    提示:若从官方源下载比较慢,可以考虑使用国内镜像源

    如阿里云:pip install setuptools -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

  2. 创建“Virtualenv Environment”虚拟环境时,勾选上“inherit global site-packages”

四、心得

遇事不要慌,先冷静分析!实在不行,先发个朋友圈也行,说不定就有朋友圈大佬给你提供远程指导哦QWQ

感谢您的阅读,我们有缘再会(下次踩坑时就会又更新了)

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值