Ubuntu18.04中安装pip出现No module named 'distutils.util'

本文记录了解决在Ubuntu系统上因Python软链接更改导致的pip安装失败问题,通过正确安装python3-pip及相关依赖,成功解决了ModuleNotFoundError错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

先按照这个博客安装的:https://www.cnblogs.com/technologylife/p/5870576.html

安装过程中出现问题:ModuleNotFoundError: No module named 'distutils.util'

按照这个博客解决的:https://blog.youkuaiyun.com/q936889811/article/details/80783427

根本原因:我觉得是因为自己把python的软链接改为了python3,而不是原来的python2

 

ningan@ningan-QiTianM610-D529:~$
ningan@ningan-QiTianM610-D529:~$ wget https://bootstrap.pypa.io/get-pip.py
--2019-08-21 16:17:37--  https://bootstrap.pypa.io/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 2a04:4e42:36::175, 151.101.108.175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|2a04:4e42:36::175|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1775411 (1.7M) [text/x-python]
正在保存至: “get-pip.py.1”

get-pip.py.1                             100%[===============================================================================>]   1.69M  1.23MB/s    用时 1.4s  

2019-08-21 16:17:40 (1.23 MB/s) - 已保存 “get-pip.py.1” [1775411/1775411])

 

ningan@ningan-QiTianM610-D529:~$ python get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 22312, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmp97they2k/pip.zip/pip/_internal/__init__.py", line 40, in <module>
  File "/tmp/tmp97they2k/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in <module>
  File "/tmp/tmp97they2k/pip.zip/pip/_internal/cli/main_parser.py", line 7, in <module>
  File "/tmp/tmp97they2k/pip.zip/pip/_internal/cli/cmdoptions.py", line 15, in <module>
ModuleNotFoundError: No module named 'distutils.util'


ningan@ningan-QiTianM610-D529:~$ pip -V     这两个命令都可以用来查看版本
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'


ningan@ningan-QiTianM610-D529:~$ pip --version
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named 'pip'

 



ningan@ningan-QiTianM610-D529:~$
ningan@ningan-QiTianM610-D529:~$ sudo apt-get install python-pip
[sudo] ningan 的密码:
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
python-pip 已经是最新版 (9.0.1-2.3~ubuntu1.18.04.1)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 64 个软件包未被升级。


ningan@ningan-QiTianM610-D529:~$ sudo apt-get install python-distutils
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
E: 无法定位软件包 python-distutils
ningan@ningan-QiTianM610-D529:~$


ningan@ningan-QiTianM610-D529:~$ sudo apt-get install python3-pip   最终解决
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  dh-python libpython3-dev libpython3.6-dev python3-dev python3-distutils python3-lib2to3 python3-setuptools python3-wheel python3.6-dev
建议安装:
  python-setuptools-doc
下列【新】软件包将被安装:
  dh-python libpython3-dev libpython3.6-dev python3-dev python3-distutils python3-lib2to3 python3-pip python3-setuptools python3-wheel python3.6-dev
升级了 0 个软件包,新安装了 10 个软件包,要卸载 0 个软件包,有 64 个软件包未被升级。
需要下载 46.0 MB 的归档。
解压缩后会消耗 81.4 MB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-lib2to3 all 3.6.8-1~18.04 [76.5 kB]
获取:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.8-1~18.04 [141 kB]
获取:3 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 dh-python all 3.20180325ubuntu2 [89.2 kB]
获取:4 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-dev amd64 3.6.8-1~18.04.1 [44.8 MB]
获取:5 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3-dev amd64 3.6.7-1~18.04 [7,328 B]                                               
获取:6 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3.6-dev amd64 3.6.8-1~18.04.1 [508 kB]                                               
获取:7 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-dev amd64 3.6.7-1~18.04 [1,288 B]                                                  
获取:8 http://cn.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-pip all 9.0.1-2.3~ubuntu1.18.04.1 [114 kB]                                     
获取:9 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 python3-setuptools all 39.0.1-2 [248 kB]                                                           
获取:10 http://cn.archive.ubuntu.com/ubuntu bionic/universe amd64 python3-wheel all 0.30.0-0.2 [36.5 kB]                                                        
已下载 46.0 MB,耗时 8秒 (5,511 kB/s)                                                                                                                           
正在选中未选择的软件包 python3-lib2to3。
(正在读取数据库 ... 系统当前共安装有 181138 个文件和目录。)
正准备解包 .../0-python3-lib2to3_3.6.8-1~18.04_all.deb  ...
正在解包 python3-lib2to3 (3.6.8-1~18.04) ...
正在选中未选择的软件包 python3-distutils。
正准备解包 .../1-python3-distutils_3.6.8-1~18.04_all.deb  ...
正在解包 python3-distutils (3.6.8-1~18.04) ...
正在选中未选择的软件包 dh-python。
正准备解包 .../2-dh-python_3.20180325ubuntu2_all.deb  ...
正在解包 dh-python (3.20180325ubuntu2) ...
正在选中未选择的软件包 libpython3.6-dev:amd64。
正准备解包 .../3-libpython3.6-dev_3.6.8-1~18.04.1_amd64.deb  ...
正在解包 libpython3.6-dev:amd64 (3.6.8-1~18.04.1) ...
正在选中未选择的软件包 libpython3-dev:amd64。
正准备解包 .../4-libpython3-dev_3.6.7-1~18.04_amd64.deb  ...
正在解包 libpython3-dev:amd64 (3.6.7-1~18.04) ...
正在选中未选择的软件包 python3.6-dev。
正准备解包 .../5-python3.6-dev_3.6.8-1~18.04.1_amd64.deb  ...
正在解包 python3.6-dev (3.6.8-1~18.04.1) ...
正在选中未选择的软件包 python3-dev。
正准备解包 .../6-python3-dev_3.6.7-1~18.04_amd64.deb  ...
正在解包 python3-dev (3.6.7-1~18.04) ...
正在选中未选择的软件包 python3-pip。
正准备解包 .../7-python3-pip_9.0.1-2.3~ubuntu1.18.04.1_all.deb  ...
正在解包 python3-pip (9.0.1-2.3~ubuntu1.18.04.1) ...
正在选中未选择的软件包 python3-setuptools。
正准备解包 .../8-python3-setuptools_39.0.1-2_all.deb  ...
正在解包 python3-setuptools (39.0.1-2) ...
正在选中未选择的软件包 python3-wheel。
正准备解包 .../9-python3-wheel_0.30.0-0.2_all.deb  ...
正在解包 python3-wheel (0.30.0-0.2) ...
正在设置 python3-wheel (0.30.0-0.2) ...
正在设置 libpython3.6-dev:amd64 (3.6.8-1~18.04.1) ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
正在设置 python3.6-dev (3.6.8-1~18.04.1) ...
正在设置 python3-lib2to3 (3.6.8-1~18.04) ...
正在设置 python3-distutils (3.6.8-1~18.04) ...
正在设置 libpython3-dev:amd64 (3.6.7-1~18.04) ...
正在设置 python3-pip (9.0.1-2.3~ubuntu1.18.04.1) ...
正在设置 python3-setuptools (39.0.1-2) ...
正在设置 dh-python (3.20180325ubuntu2) ...
正在设置 python3-dev (3.6.7-1~18.04) ...
ningan@ningan-QiTianM610-D529:~$
ningan@ningan-QiTianM610-D529:~$
ningan@ningan-QiTianM610-D529:~$
ningan@ningan-QiTianM610-D529:~$ pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
ningan@ningan-QiTianM610-D529:~$

 

pip install numpy==1.16.6 -i https://pypi.tuna.tsinghua.edu.cn/simple Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting numpy==1.16.6 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b7/6f/24647f014eef9b67a24adfcbcd4f4928349b4a0f8393b3d7fe648d4d2de3/numpy-1.16.6.zip (5.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.1/5.1 MB 910.1 kB/s eta 0:00:00 Preparing metadata (setup.py) ... done Building wheels for collected packages: numpy Building wheel for numpy (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [17 lines of output] Running from numpy source directory. /tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/numpy/distutils/misc_util.py:476: SyntaxWarning: "is" with a literal. Did you mean "=="? return is_string(s) and ('*' in s or '?' is s) Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "/tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/setup.py", line 419, in <module> setup_package() File "/tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/setup.py", line 398, in setup_package from numpy.distutils.core import setup File "/tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/numpy/distutils/core.py", line 26, in <module> from numpy.distutils.command import config, config_compiler, \ File "/tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/numpy/distutils/command/config.py", line 19, in <module> from numpy.distutils.mingw32ccompiler import generate_manifest File "/tmp/pip-install-4mvefocn/numpy_ba42b718220646d29282bcb90bdda552/numpy/distutils/mingw32ccompiler.py", line 34, in <module> from distutils.msvccompiler import get_build_version as get_build_msvc_version ModuleNotFoundError: No module named 'distutils.msvccompiler' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for numpy Running setup.py clean for numpy error: subprocess-exited-with-error × python setup.py clean did not run successfully. │ exit code: 1 ╰─> [10 lines of output] Running from numpy source directory. `setup.py clean` is not supported, use one of the following instead: - `git clean -xdf` (cleans all files) - `git clean -Xdf` (cleans all versioned files, doesn't touch files that aren't checked into the git repo) Add `--force` to your command to use it anyway if you must (unsupported). [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed cleaning build dir for numpy Failed to build numpy ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (numpy) 怎么解决
06-03
### 解决方案 当在 Conda 环境下安装 `numpy` 出现 `ModuleNotFoundError: No module named 'distutils.msvccompiler'` 错误时,这通常是因为 Python 的标准库模块 `distutils` 缺失或损坏。以下是可能的原因分析和对应的解决办法: #### 原因分析 1. **Python 版本问题** 部分高版本的 Python 可能会移除或修改某些默认的标准库模块,例如 `distutils` 已被标记为废弃并计划在未来版本中删除[^4]。 2. **环境配置不完整** 升级操作系统或其他操作可能导致依赖项丢失或损坏,比如从 Ubuntu 18.04 到 20.04 的升级过程中可能会引发此类问题[^2]。 3. **编译工具链缺失** 在 Windows 平台上,缺少 Microsoft Visual C++ (MSVC) 组件也可能导致类似的错误消息[^5]。 --- #### 解决方法 ##### 方法一:重新安装 `distutils` 可以通过以下命令修复或重装 `distutils` 模块: ```bash sudo apt-get install python3-distutils ``` 此方法适用于基于 Debian/Ubuntu 的 Linux 发行版。如果是在其他平台上,则需找到对应的操作系统软件包管理器来完成安装。 ##### 方法二:更新或重建 Conda 环境 Conda 是一个独立于系统的包管理和虚拟环境管理系统,因此可以直接通过它来解决问题: ```bash conda update conda conda update --all ``` 接着创建一个新的干净环境,并在此环境中安装所需的包: ```bash conda create -n new_env python=3.9 conda activate new_env conda install numpy ``` 这里建议指定较低版本的 Python(如 3.7 或 3.9),因为这些版本更稳定且兼容性强。 ##### 方法三:手动安装 `pylzma` 和相关依赖 对于特定情况下仍无法正常工作的场景,可考虑单独处理有问题的依赖关系。例如针对 `pylzma` 报错的情况,按照官方文档指引下载其源码并执行本地构建过程[^3]: ```bash wget https://files.pythonhosted.org/packages/source/p/pylzma/pylzma-<version>.tar.gz tar zxvf pylzma-<version>.tar.gz cd pylzma-<version> python setup.py build python setup.py install ``` ##### 方法四:检查 MSVC 安装状态 如果是运行在 Windows 上遇到该类问题,请确认已经正确安装了最新版的 Microsoft Visual Studio,并确保选择了“C++ 构建工具”工作负载选项。之后再试一次 pip 或者 conda 来安装目标库文件即可恢复正常运作流程。 --- ### 总结 上述四种方式涵盖了不同平台下的多种可能性及其应对策略。优先推荐使用 Conda 自带的功能来进行调整优化;而对于特殊需求则采取针对性更强的手动干预措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安安csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值