首页 > 解决方案 > 错误:[.venv/bin/python3.9', '-Im', 'ensurepip', '--upgrade', '--default-pip']' 返回非零退出状态 1,尽管 python3.9-已安装 venv

问题描述

我已经在 deadsnakes PPA 的许多 linux OS 版本上安装了许多 python 版本,没有问题。

但是,我遇到了一个问题,python3.9 -m venv .venv尽管安装了 python3.9-venv 并且在安装过程中没有遇到任何问题,但我无法使用它创建虚拟环境。

ac@LLB-226500:~$ apt list --installed | grep python3*

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libpython3-dev/focal,now 3.8.2-0ubuntu2 amd64 [installed,automatic]
libpython3-stdlib/focal,now 3.8.2-0ubuntu2 amd64 [installed,automatic]
libpython3.10-minimal/focal,now 3.10.0-1+focal1 amd64 [installed,auto-removable]
libpython3.10-stdlib/focal,now 3.10.0-1+focal1 amd64 [installed,auto-removable]
libpython3.8-dev/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
libpython3.8-minimal/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
libpython3.8-stdlib/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
libpython3.8/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
libpython3.9-dev/focal,now 3.9.7-1+focal1 amd64 [installed,automatic]
libpython3.9-minimal/focal,now 3.9.7-1+focal1 amd64 [installed,automatic]
libpython3.9-stdlib/focal,now 3.9.7-1+focal1 amd64 [installed,automatic]
libpython3.9/focal,now 3.9.7-1+focal1 amd64 [installed,automatic]
python-apt-common/focal,now 2.0.0 all [installed,upgradable to: 2.0.0ubuntu0.20.04.6]
python-pip-whl/focal-updates,now 20.0.2-5ubuntu1.6 all [installed,automatic]
python3-apport/focal,now 2.20.11-0ubuntu27 all [installed,upgradable to: 2.20.11-0ubuntu27.21]
python3-apt/focal,now 2.0.0 amd64 [installed,upgradable to: 2.0.0ubuntu0.20.04.6]
python3-attr/focal,now 19.3.0-2 all [installed,automatic]
python3-automat/focal,now 0.8.0-1ubuntu1 all [installed,automatic]
python3-blinker/focal,now 1.4+dfsg1-0.3ubuntu1 all [installed,automatic]
python3-certifi/focal,now 2019.11.28-1 all [installed,automatic]
python3-cffi-backend/focal,now 1.14.0-1build1 amd64 [installed,automatic]
python3-chardet/focal,now 3.0.4-4build1 all [installed,automatic]
python3-click/focal,now 7.0-3 all [installed,automatic]
python3-colorama/focal,now 0.4.3-1build1 all [installed,automatic]
python3-commandnotfound/focal,now 20.04.2 all [installed,upgradable to: 20.04.4]
python3-configobj/focal,now 5.0.6-4 all [installed,automatic]
python3-constantly/focal,now 15.1.0-1build1 all [installed,automatic]
python3-cryptography/focal,now 2.8-3 amd64 [installed,upgradable to: 2.8-3ubuntu0.1]
python3-dbus/focal,now 1.2.16-1build1 amd64 [installed,automatic]
python3-debconf/focal,now 1.5.73 all [installed,automatic]
python3-debian/focal,now 0.1.36ubuntu1 all [installed,automatic]
python3-dev/focal,now 3.8.2-0ubuntu2 amd64 [installed,automatic]
python3-distro-info/focal,now 0.23ubuntu1 all [installed,automatic]
python3-distro/focal,now 1.4.0-1 all [installed,automatic]
python3-distupgrade/focal,now 1:20.04.18 all [installed,upgradable to: 1:20.04.36]
python3-distutils/focal,now 3.8.2-1ubuntu1 all [installed,upgradable to: 3.8.10-0ubuntu1~20.04]
python3-entrypoints/focal,now 0.3-2ubuntu1 all [installed,automatic]
python3-gdbm/focal,now 3.8.2-1ubuntu1 amd64 [installed,upgradable to: 3.8.10-0ubuntu1~20.04]
python3-gi/focal,now 3.36.0-1 amd64 [installed,automatic]
python3-hamcrest/focal,now 1.9.0-3 all [installed,automatic]
python3-httplib2/focal,now 0.14.0-1ubuntu1 all [installed,automatic]
python3-hyperlink/focal,now 19.0.0-1 all [installed,automatic]
python3-idna/focal,now 2.8-1 all [installed,automatic]
python3-importlib-metadata/focal,now 1.5.0-1 all [installed]
python3-incremental/focal,now 16.10.1-3.2 all [installed,automatic]
python3-jinja2/focal,now 2.10.1-2 all [installed]
python3-json-pointer/focal,now 2.0-0ubuntu1 all [installed]
python3-jsonpatch/focal,now 1.23-3 all [installed]
python3-jsonschema/focal,now 3.2.0-0ubuntu2 all [installed]
python3-jwt/focal,now 1.7.1-2ubuntu2 all [installed,automatic]
python3-keyring/focal,now 18.0.1-2ubuntu1 all [installed,automatic]
python3-launchpadlib/focal,now 1.10.13-1 all [installed,automatic]
python3-lazr.restfulclient/focal,now 0.14.2-2build1 all [installed,automatic]
python3-lazr.uri/focal,now 1.0.3-4build1 all [installed,automatic]
python3-lib2to3/focal,now 3.8.2-1ubuntu1 all [installed,upgradable to: 3.8.10-0ubuntu1~20.04]
python3-markupsafe/focal,now 1.1.0-1build2 amd64 [installed]
python3-minimal/focal,now 3.8.2-0ubuntu2 amd64 [installed,automatic]
python3-more-itertools/focal,now 4.2.0-1build1 all [installed]
python3-nacl/focal,now 1.3.0-5 amd64 [installed]
python3-netifaces/focal,now 0.10.4-1ubuntu4 amd64 [installed,automatic]
python3-newt/focal,now 0.52.21-4ubuntu2 amd64 [installed,automatic]
python3-oauthlib/focal,now 3.1.0-1ubuntu2 all [installed,automatic]
python3-openssl/focal,now 19.0.0-1build1 all [installed,automatic]
python3-pip/focal-updates,now 20.0.2-5ubuntu1.6 all [installed]
python3-pkg-resources/focal,now 45.2.0-1 all [installed,automatic]
python3-problem-report/focal,now 2.20.11-0ubuntu27 all [installed,upgradable to: 2.20.11-0ubuntu27.21]
python3-pyasn1-modules/focal,now 0.2.1-0.2build1 all [installed,automatic]
python3-pyasn1/focal,now 0.4.2-3build1 all [installed,automatic]
python3-pymacaroons/focal,now 0.13.0-3 all [installed]
python3-pyrsistent/focal,now 0.15.5-1build1 amd64 [installed]
python3-requests-unixsocket/focal,now 0.2.0-2 all [installed,automatic]
python3-requests/now 2.22.0-2build1 all [installed,upgradable to: 2.22.0-2ubuntu1]
python3-secretstorage/focal,now 2.3.1-2ubuntu1 all [installed,automatic]
python3-serial/focal,now 3.4-5.1 all [installed]
python3-service-identity/focal,now 18.1.0-5build1 all [installed,automatic]
python3-setuptools/focal,now 45.2.0-1 all [installed]
python3-simplejson/focal,now 3.16.0-2ubuntu2 amd64 [installed,automatic]
python3-six/focal,now 1.14.0-2 all [installed,automatic]
python3-software-properties/focal-updates,now 0.98.9.5 all [installed,automatic]
python3-systemd/focal,now 234-3build2 amd64 [installed,automatic]
python3-twisted-bin/focal,now 18.9.0-11 amd64 [installed,upgradable to: 18.9.0-11ubuntu0.20.04.1]
python3-twisted/focal,now 18.9.0-11 all [installed,upgradable to: 18.9.0-11ubuntu0.20.04.1]
python3-update-manager/focal,now 1:20.04.9 all [installed,upgradable to: 1:20.04.10.9]
python3-urllib3/focal,now 1.25.8-2 all [installed,upgradable to: 1.25.8-2ubuntu0.1]
python3-venv/focal,now 3.8.2-0ubuntu2 amd64 [installed]
python3-wadllib/focal,now 1.3.3-3build1 all [installed,automatic]
python3-wheel/focal,now 0.34.2-1 all [installed,automatic]
python3-yaml/focal,now 5.3.1-1 amd64 [installed,upgradable to: 5.3.1-1ubuntu0.1]
python3-zipp/focal,now 1.0.0-1 all [installed]
python3-zope.interface/focal,now 4.7.1-1 amd64 [installed,automatic]
python3.8-dev/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
python3.8-minimal/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
python3.8-venv/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
python3.8/focal-updates,focal-security,now 3.8.10-0ubuntu1~20.04.1 amd64 [installed,automatic]
python3.9-dev/focal,now 3.9.7-1+focal1 amd64 [installed]
python3.9-minimal/focal,now 3.9.7-1+focal1 amd64 [installed,automatic]
python3.9-venv/focal,now 3.9.7-1+focal1 amd64 [installed]
python3.9/focal,now 3.9.7-1+focal1 amd64 [installed]
python3/focal,now 3.8.2-0ubuntu2 amd64 [installed,automatic]
ac@LLB-226500:~$ python3.8 -m venv .venv && rm -rf .venv
ac@LLB-226500:~$ python3.9 -m venv .venv
Error: Command '['/home/ac/.venv/bin/python3.9', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

如果相关,我首先安装了 python3.10(和 python3.10-venv),它工作得非常好,但我意识到我的一些项目依赖项还不支持 3.10,所以我降级到 3.9,然后这就是我被困。

编辑

ac@LLB-226500:~$ python3.9 -Im ensurepip --upgrade --default-pip -vvv
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "/usr/lib/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/__main__.py", line 29, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/cli/main.py", line 9, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/cli/autocompletion.py", line 10, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/cli/main_parser.py", line 8, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/cli/cmdoptions.py", line 23, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/cli/parser.py", line 12, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/configuration.py", line 27, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/utils/misc.py", line 42, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/locations/__init__.py", line 14, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl/pip/_internal/locations/_distutils.py", line 9, in <module>
ModuleNotFoundError: No module named 'distutils.cmd'
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.9/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/usr/lib/python3.9/ensurepip/__init__.py", line 210, in _main
    return _bootstrap(
  File "/usr/lib/python3.9/ensurepip/__init__.py", line 129, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/usr/lib/python3.9/ensurepip/__init__.py", line 38, in _run_pip
    return subprocess.run([sys.executable, "-c", code], check=True).returncode
  File "/usr/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/bin/python3.9', '-c', '\nimport runpy\nimport sys\nsys.path = [\'/tmp/tmp_iumbc2_/setuptools-57.4.0-py3-none-any.whl\', \'/tmp/tmp_iumbc2_/pip-21.2.3-py3-none-any.whl\'] + sys.path\nsys.argv[1:] = [\'install\', \'--no-cache-dir\', \'--no-index\', \'--find-links\', \'/tmp/tmp_iumbc2_\', \'--upgrade\', \'-vvv\', \'setuptools\', \'pip\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.

标签: pythonubuntu-20.04

解决方案


我的特定问题已通过以下方式解决:

sudo apt-get install python3-distutils

推荐阅读