在 Debian/Ubuntu 中为系统 python 禁用了 ensurepip

Ann*_*ine 15 python 16.04

我正在尝试为我的 Django 应用程序的开发创建一个虚拟环境。我正在使用的突击队:

vagrant@vagrant:/var/www/djangogirls$ python3 -m venv myvenv
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: ['/var/www/djangogirls/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']


vagrant@vagrant:/var/www/djangogirls$ sudo apt-get install python3-venv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-venv is already the newest version (3.5.1-3).
The following packages were automatically installed and are no longer required:
  javascript-common libjs-jquery libjs-sphinxdoc libjs-underscore python-pbr python-pkg-resources
  python-six python-stevedore python3-virtualenv virtualenv virtualenv-clone
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 108 not upgraded.

vagrant@vagrant:/var/www/djangogirls$ python3 -m ensurepip
ensurepip is disabled in Debian/Ubuntu for the system python.

Python modules for the system python are usually handled by dpkg and apt-get.

    apt-get install python-<module name>

Install the python-pip package to use pip itself.  Using pip together
with the system python might have unexpected results for any system installed
module, so use it on your own risk, or make sure to only use it in virtual
environments.


vagrant@vagrant:/var/www/djangogirls$ rm -r myvenv/ 

vagrant@vagrant:/var/www/djangogirls$ python3 -m venv myvenv
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: ['/var/www/djangogirls/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在尝试创建一个 myvenv,但由于缺少 python3-venv 而无法创建。我已经安装了它,但缺少确保 pip。搜索后似乎系统(Ubuntu 16.04)不鼓励使用该软件包。有人可以帮我解决这个问题吗?

小智 20

有一个相关的错误报告here

Ubuntu 上的ensurepip组件丢失/禁用

解决方法是创建一个没有pip的虚拟环境

python3 -m venv myvenv --without-pip
Run Code Online (Sandbox Code Playgroud)

在这种情况下,不会调用ensurepip组件并创建一个新环境。

然而,在虚拟环境中缺少 pip 可能是一个问题。

一种解决方案是安装系统 pip3 包并直接在您的虚拟环境中使用系统 pip 模块。

虚拟环境必须能够访问系统站点包才能使用系统 pip 模块。

  1. 安装系统python3 pip包

    sudo apt-get install python3-pip
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建没有 pip 并且可以访问系统站点包的虚拟环境

    python3 -m venv myvenv --without-pip --system-site-packages
    
    Run Code Online (Sandbox Code Playgroud)

您现在可以使用系统 pip 模块将 python 包安装到您的虚拟环境中。

而不是pip install Django你必须使用显式

myvenv/bin/python3 -m pip install Django
Run Code Online (Sandbox Code Playgroud)

或者您可以先激活您的虚拟环境

source myvenv/bin/activate
python3 -m pip install Django
Run Code Online (Sandbox Code Playgroud)

python3 -m pip --version 可以方便地查看使用了哪个 python 环境。

基于这里找到的解决方案,但不要python get-pip.py在虚拟环境中使用建议,因为它会窃取系统pip命令


小智 7

我遇到了同样的问题,python3-venv按照说明安装并没有解决ensurepip不可用问题!但是,由于我的 python3 版本是3.7.5 installingpython3.7-venv` 相反解决了我的问题。