【问题标题】:How to install python-distutils for old python versions如何为旧的 python 版本安装 python-distutils
【发布时间】:2020-08-17 10:45:57
【问题描述】:

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

我正在尝试使用“python3.7 -m pip install package”在 3.6 和 3.7 版本上使用 pip 安装一些软件包,但是,我收到了这个错误:

ModuleNotFoundError: No module named 'distutils.util

我已经安装了python3-distutilspython3-distutils-extrapip 仅适用于python 3.8

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

【问题讨论】:

标签: python ubuntu pip distutils ubuntu-20.04


【解决方案1】:

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

警告,我可能搞砸了我的 python3.8 安装作为副作用,并且可能会再次卸载该软件包并尽快破坏当你运行 apt-update 时。

另外,也许可以减少命令的数量,但这是我最终做的:

运行 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包, 因为它实际上包含多个版本的distutils, 对于 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) ...

至此,我终于可以在python3.6中使用pip了。


注 1: 以上apt list -a 的打印输出取决于您运行的是 Ubuntu 还是 Debian 发行版,以及哪个适合-repos 您已在 /etc/apt/sources.list* 中激活。

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

注意 3:如果您找不到特定 Python 版本的确切 distutils 版本,您可以安装较早的版本并对其进行符号链接。 例如,截至 2020 年 1 月,在 Debian-unstable("sid") 和 "bullseye" 中,它们的 apt-repos 中包含的 python3-distutils 包包含仅适用于 Python3.9 的文件,而之前来自 "buster" 的 apt-repos 包含仅适用于 Python3.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/

【讨论】:

    【解决方案2】:

    (来自赏金描述)

    我想要一个明确的解决方案,不需要有人告诉我安装:

    • python-distutils
    • python3-distutils
    • python3-distutils-extra

    我将向提到安装这些的任何人分发免费的 DOWNVOTES。

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

    这是 Debian 决定将 Python 标准库的各个部分分解为单独的包的结果,基于他们将事物拆分为运行时包和开发包的政策。他们认为 distutils 属于“开发”部分,因此,不要将其作为标准 python 包的一部分分发。


    基本上,您的选择是:

    • 安装python*-distutils 包。
      • 这是您的操作系统维护人员建议在您的情况下执行的操作。
      • 您没有解释为什么您会为任何推荐此方法的人“免费投反对票”,但这确实是“最正确”的解决方案。
    • 从不破坏标准库的地方安装 Python。
      • 这意味着使用官方 debian 存储库以外的包源。
      • 最简单的来源是deadsnakes ppa
    • 编译你自己的Python!
      • 这实际上并不难,pyenv 等工具可以轻松编译和管理多个 python 版本。
    • 破解这个问题!
      • 你可以...从 CPython 的 repo 下载源代码,并将 Lib/distutils 文件夹放在 pythonX.Y 的导入路径上的某个位置?这是一个 100% 的 hack,我强烈建议不要这样做。如果发生了不好的事情,因为你这样做了,我概不负责。

    【讨论】:

    • 我已经安装了这些软件包,但它们不适用于 旧的 python 版本 - 因此我很沮丧。 bionic file list 包含 python 3.6 和 3.7 所需的软件包,但这对我没有好处,因为我安装的 focal file list 没有。有没有办法两者兼得?
    • 我对问题进行了编辑,以便更清楚地了解问题到底是什么。在 Ubuntu 20.04 上让 pip 使用 python3.8 非常非常容易。到目前为止,让它与 python3.7 一起工作是不可能的。我挑战你自己尝试一下,以便更好地理解问题。
    • 非常感谢deadsnakes ppa。我已经在 Debian Bullseye 上安装(测试),并且我的 py 3.7 再次工作(与系统 py 3.8 一起)。
    【解决方案3】:

    deadsnakespyenv 之类的东西怎么样,这些有帮助吗?否则,您可能需要为旧 Ubuntu 版本(例如 18.04)添加 apt 存储库,但我不确定可能会产生什么副作用。这是我宁愿在Ask UbuntuSuper User 上问的问题。

    【讨论】:

    • 我的虚拟环境在操作系统升级到较新版本的 LinuxMint 后变得无法使用。使用 pyenv 安装 python 3.6 然后重新创建我的虚拟环境为我解决了这个问题
    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 2011-04-18
    • 2019-12-24
    • 2015-03-01
    • 2020-02-16
    • 2023-03-05
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多