在系统更新到 Ubuntu 20.04 后,我遇到了各种与 Python 相关的问题。当前的问题是我无法创建虚拟环境:
python3 -m venv env
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/XXX/my-project/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
为了进一步了解失败的原因,我运行了
./env/bin/python3 -Im ensurepip --upgrade --default-pip
:
/env/bin/python3: Error while finding module specification for 'ensurepip.__main__' (ImportError: cannot import name '_bundled' from partially initialized module 'ensurepip' (most likely due to a circular import) (/usr/lib/python3.9/ensurepip/__init__.py))
当然,我尝试了建议的步骤sudo apt-get install python3-venv
:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
python3-venv : Depends: python3.8-venv (>= 3.8.2-1~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
lsb_release -a
输出:
描述:Ubuntu 20.04.1
LTS 版本:20.04
代号:focal
whereis python
输出:
python: /usr/bin/python3.6 /usr/bin/python3.7 /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python3.6m /usr/bin/python3.9 /usr/bin/python3.8 /usr/bin/python /usr/bin/python3.7m /usr/lib/python3.6 /usr/lib/python3.7 /usr/lib/python2.7 /usr/lib/python3.9 /usr/lib/python3.8 /etc/python3.6 /etc/python3.7 /etc/python2.7 /etc/python3.9 /etc/python3.8 /etc/python /usr/local/lib/python3.6 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/local/lib/python3.9 /usr/local/lib/python3.8 /usr/include/python3.6 /usr/include/python2.7 /usr/include/python3.6m /usr/include/python3.8 /usr/share/python
/etc/apt/sources.list
仅包含焦点软件源。
也跑了
sudo apt update
sudo apt upgrade
删除了 python3.9,sudo apt install python3.8-venv
失败并出现同样的错误
python3.8-venv:依赖:python3.8(= 3.8.5-1~20.04)但需要安装 3.8.6-1+bionic1 E:无法纠正问题,您持有损坏的软件包。
dpkg --get-selections | grep hold
返回空
答案1
查看您提供的信息,您的问题可能是由于 python3.9 安装造成的。
在 Ubuntu 20.04 中,默认的 Python 版本是 3.8。因此,依赖于 Python 的其他软件包应该需要 Python3.8。
但是,在您的系统中,此默认值已更改为 Python 3.9,因为尝试创建新的 venv 最终得到了 Python 3.9 环境,如输出中所示。
正如您原来的错误所提到的,您需要python3-venv
使用 安装包apt
。但由于也安装了 python3.9,我相信apt
无法再确定要安装什么。因此明确提及完整版本可能会解决您的问题。
sudo apt install python3.8-venv
參考文獻:apt-get,未满足的依赖关系,......“但它不会被安装”
完成后,你还需要通过明确传递完整的 Python 版本来创建 venv
python3.8 -m venv env
如果你打算使用 Python 3.9,并且只要它是使用 进行安装的apt
,你可以通过安装python3.9-venv
而不是python3.8-venv
sudo apt install python3.9-venv
附言:安装 apt 包后,您需要再次创建一个新的虚拟环境。之前的虚拟环境将保持损坏状态。
答案2
我遇到了类似的问题。我无法安装python3.8-venv
软件包,因为python3.8
软件包的版本较新(后缀 ~20.04)。结果发现我没有焦点安全我的 source.list 中的来源。
添加deb http://security.ubuntu.com/ubuntu focal-security main universe
后/etc/apt/sources.list
问题得到解决。
答案3
答案4
原来我不得不继续删除各种仿生的python 包:
sudo apt remove python3.8
sudo apt remove python3.8-minimal
sudo apt remove libpython3.8-stdlib
sudo apt remove libpython3.8-minimal
每当它抱怨他们时……
The following packages have unmet dependencies:
python3.8 : Depends: python3.8-minimal (= 3.8.5-1~20.04) but 3.8.6-1+bionic1 is to be installed
Depends: libpython3.8-stdlib (= 3.8.5-1~20.04) but 3.8.6-1+bionic1 is to be installed
...我把它们删掉了
...直到我可以(重新)安装这些:
sudo apt install python3.8-minimal
sudo apt install python3.8
sudo apt install python3.8-venv
sudo apt install python3-distutils
我终于可以跑了python3.8 -m venv env
因此,从仿生海狸到局灶性窝部分遗留了旧的 Python 包,从而导致了不兼容。