如何为旧版本的python安装python distutils



我运行的是安装了python 3.6、3.7和3.8的Ubuntu 20.04。

我试图在3.6和3.7版本上使用pip安装一些软件包,使用"python3.7-m pip安装软件包",但是,我得到了这个错误:

ModuleNotFoundError: No module named 'distutils.util

我已经安装了python3-distutilspython3-distutils-extra,但pip仅适用于python 3.8

如何让pip在python 3.6和3.7上安装软件包

像死蛇或pyenv这样的东西会有帮助吗?否则,你可能需要为旧的Ubuntu版本(比如18.04)添加apt存储库,但我不确定可能会有什么副作用。这是我宁愿在ask Ubuntu或Super User上问的问题。

(来自赏金描述)

我想要一个不需要别人告诉我安装的最终解决方案:

  • python distutils
  • 蟒蛇3 distutils
  • 蟒蛇3 distutils额外

我将向任何提到安装这些的人免费提供下载。

好吧,我有一些坏消息要告诉你。在不安装这些包的情况下,没有"官方"方法可以在Debian提供的python*包上获得distutils,除非你离开Debian的默认包存储库。

这是Debian决定将Python标准库的各个部分分解为单独的包的结果,基于他们将东西分解为运行时包和开发包的策略。他们认为distutils属于"开发"部分,因此,不将其作为标准python包的一部分进行分发。


基本上,您的选择是:

  • 安装一个python*-distutils软件包。
    • 这是您的操作系统维护人员在您的情况下建议的操作
    • 你没有解释为什么你会"免费向任何推荐的人发放否决票",但这确实是"最正确"的解决方案
  • 从不破坏标准库的地方安装Python。
    • 这意味着使用除官方debian存储库之外的包源
    • 最简单的来源是死蛇ppa
  • 编译您自己的Python!
    • 这实际上并没有那么困难,像pyenv这样的工具使编译和管理多个python版本变得容易
  • 破解这个问题!
    • 你可以。。。从CPython的repo下载源,并将Lib/distutils文件夹放在pythonX.Y的导入路径上的某个位置?这是一个100%的黑客攻击,我强烈建议不要这样做。如果因为你这样做而发生了不好的事情,我不负责任

我和你一样一直在寻找这个问题的答案,终于找到了解决方案。

请注意,我可能会因为副作用而破坏我的python3.8安装,并且一旦运行apt更新,该软件包可能会再次被卸载并破坏。

此外,减少命令的数量也是可能的,但我最终做的是:

运行apt list查看可用软件包列表:

$ apt list -a python3-distutils
Listing... Done
python3-distutils/focal-updates,focal-updates,focal-security,focal-security,now 3.8.5-1~20.04.1 all [installed]
python3-distutils/focal,focal 3.8.2-1ubuntu1 all
python3-distutils/bionic-updates,bionic-updates 3.6.9-1~18.04 all
python3-distutils/bionic,bionic 3.6.5-3 all

然后下载";仿生的";deb包,因为它实际上包含CCD_ 10的多个版本,对于Python 3.6、3.7&3.8:

$ apt download python3-distutils/bionic
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.9-1~18.04 [144 kB]
Fetched 144 kB in 0s (661 kB/s)

最后,安装deb包:

$ sudo dpkg --install python3-distutils_3.6.9-1~18.04_all.deb 
dpkg: warning: downgrading python3-distutils from 3.8.5-1~20.04.1 to 3.6.9-1~18.04
(Reading database ... 193375 files and directories currently installed.)
Preparing to unpack python3-distutils_3.6.9-1~18.04_all.deb ...
Unpacking python3-distutils (3.6.9-1~18.04) over (3.8.5-1~20.04.1) ...
Setting up python3-distutils (3.6.9-1~18.04) ...

在这一点上,我终于能够将pip与python3.6一起使用了。


注1:以上apt list -a的打印结果取决于您是运行Ubuntu还是运行Debian发行版,以及您在/etc/apt/sources.list*中激活了哪些apt-respo。

注2:如上文python3-distutils/bionic的情况所示,包的名称并不总是与其内容一致。要查看它们,您必须下载并使用dpkg -C <pafkage-file>进行检查,或者使用apt-file list python3-distutils从远程进行检查。

注意-3:如果您找不到特定Python版本的精确distutils版本,您可以安装早期版本并与其符号链接。例如在Debian不稳定("sid")和;牛眼;截至2020年1月,他们的apt-reos中包含的python3-distutils包有仅适用于Python3.9的文件;buster";仅包含Python 3.7的文件。因此,如果您希望为Python3.8安装distutils,您必须下载;buster";打包并执行:

$ sudo dpkg --unpack python3-distutils_3.7.3-1_all.deb 
$ sudo rm /usr/lib/python3.8/distutils/distutils/
$ sudo ln -w /usr/lib/python3.{7,8}/distutils/distutils/

此处的说明对我有效:https://www.linuxcapable.com/how-to-install-python-3-7-on-ubuntu-20-04-lts/

也就是这个sudo apt install python3.7-distutils

正在尝试安装checkov并获取ModuleNotFound错误。在上面之后,它按照的这些说明安装得很好

以下是我在Ubuntu 22.04 for Python 3.6上所做的工作。pip不会安装,因为distutils不可用。

  • 通过apt安装python3.6
  • 使用python3.6 --version检查确切版本
  • 在此处查找匹配的标签:https://github.com/python/cpython.git,即3.6.9
  • 调整并调用git clone --depth=1 -b v3.6.9 https://github.com/python/cpython.git
  • export PYTHONPATH=`pwd`/cpython/Lib
  • curl https://bootstrap.pypa.io/pip/3.6/get-pip.py | python3.6 - --user
  • python3.6 -m pip list

如果旧版本不可用,请将cd放入cpython源目录,并使用例如./configure --enable-optimizations --prefix=$HOME/.localmake -j4make altinstall(在调用configure时设置一些PREFIX以安装,这样它不会破坏系统,但请注意,安装路径必须在搜索路径中)

最新更新