ModuleNotFoundError:没有名为“distutils.util”的模块

ModuleNotFoundError:没有名为“distutils.util”的模块

更新到 Ubuntu 20.04 后,每当我运行:

pip install <some package>

或者

pip --version

它说:

adam@daryy:~$ pip3

Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'

即使我通常输入pip3pip

答案1

升级到 Ubuntu 20.04 后,我遇到了这个问题。我有一个依赖于 Python 3.7 的虚拟环境,为了避免重新安装 Python 3.8 的整个虚拟环境,我distutils在 Python 3.7 上进行了修复:

我添加了死蛇 PPA

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

然后安装distutils适用于 Python 3.7 的包:

sudo apt install python3.7-distutils

注意:由于某种原因,我在安装后者时出错,我解决了这边走

sudo dpkg -i --force-overwrite /var/cache/apt/archives/python3.7-distutils_3.7.9-1+focal1_all.deb
sudo apt-get -f install 

答案2

未找到模块可能意味着软件包未安装。

sudo apt-get install python3-distutils

sudo apt-get install python3-apt

如果它们已经安装,您可以尝试修复升级过程中可能出现的任何问题......

sudo apt-get install --reinstall package-name

答案3

Debian 已决定 distutils 不是核心 Python 包,因此它不包含在最新版本的 Debian 和基于 Debian 的操作系统中。您应该能够这样做,sudo apt install python3-distutils并且它应该可以工作。

但是,它对我来说不起作用。我使用 Parrot OS,它和 Ubuntu 一样,基于 Debian。我升级了系统,pip 停止为 python3.7 工作,我还得到了错误ModuleNotFoundError: No module named 'distutils.util'

我尝试了很多方法来修复它并重新安装 distutils,我偶然发现了 pip3,适用于 python3.8做过工作。然后我尝试了一下python3.7 -m pip3 -V,得到了/usr/bin/python3.7: No module named pip3,所以我决定查看一下/usr/lib文件。

我看了看/usr/lib/python3/dist-packages,一切看起来都很好。然后我看了看/usr/lib/python3.7,看到了文件夹distutil

我打开它,看到了__pycache__,一个__init__.py文件和一个version.py文件。我不知道里面应该有多少个文件,也不知道代码应该是什么,但我知道这两个文件要么是错误的,要么是缺少另一个文件。

然后我查看了里面的内容/usr/lib/python3.8/distutil,发现完全不一样。我发现了以下文件:

command                          Folder
__pycache__                      Folder
archive_util.py                  Python script
bcppcompiler.py                  Python script
cmd.py                           Python script
config.py                        Python script
core.py                          Python script
cygwinccompiler.py               Python script
debug.py                         Python script
dep_util.py                      Python script
errors.py                        Python script
extension.py                     Python script
fancy_getopt.py                  Python script
filelist.py                      Python script
file_util.py                     Python script
__init__.py                      Python script
log.py                           Python script
msvc9compiler.py                 Python script
_msvccompiler.py                 Python script
msvccompiler.py                  Python script
README                           Plain text file
spawn.py                         Python script
sysconfig.py                     Python script
text_file.py                     Python script
unixccompiler.py                 Python script
util.py                          Python script
version.py                       Python script
versionpredicate.py              Python script

这是很有希望的,而且由于 pip3 确实有效,我认为这个 distutils 也能起作用,并且我尝试通过运行以下命令将其复制到 python3.7 文件夹:

sudo cp -r /usr/lib/python3.8/distutils /usr/lib/python3.7/distutils

或者:

sudo mv /usr/lib/python3.7/distutils/ /usr/lib/python3.7/distutils_back
sudo ln -s /usr/lib/python3.8/distutils /usr/lib/python3.7/

然后我又试了python3.7 -m pip -V一次

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.7)

然后我尝试安装一些模块,一切正常。希望这对您有所帮助。

答案4

确保根据python版本安装相应版本例如获取所有可用版本(假设使用apt包管理器):

$ apt-cache search distutils
python-setuptools - Python Distutils Enhancements
python-setuptools-doc - Python Distutils Enhancements (documentation)
python3-d2to1 - Python3 support for distutils2-like setup.cfg files as package metadata
python3-setuptools - Python3 Distutils Enhancements
pypy-setuptools - PyPy Distutils Enhancements
python-d2to1 - Python support for distutils2-like setup.cfg files as package metadata
python-distlib - low-level components of python distutils2/packaging
python-distutils-extra - enhancements to the Python build system
python-stsci.distutils - Python packaging utilities for STScI's packages
python3-distlib - low-level components of python distutils2/packaging
python3-distutils-extra - enhancements to the Python3 build system
python3-stdeb - Python to Debian source package conversion plugins for distutils
python3-stsci.distutils - Python3 packaging utilities for STScI's packages
python3.7-distutils - distutils package for Python (version 3.7)
python3.8-distutils - distutils package for Python (version 3.8)
python3.9-distutils - distutils package for Python (version 3.9)

您可以在上面的输出中看到python3.7-distutilspython3.8-distutilspython3.9-distutils,然后可以通过指定与您想要的 python 版本绑定的版本来安装它,例如我有python3.8并且我想要做:

$ sudo apt-get install python3.8-distutils

然后我将使用该 python 版本运行 pip(确保首先运行echo $PYTHONPATH以确认路径中的 python3.8(我的 python 版本),否则 pip 会将其安装在错误的路径中),例如:

$ python3.8 -m pip install requests

相关内容