在virtualenv中控制pip版本 [英] Control the pip version in virtualenv
问题描述
如何控制在新创建的中使用的pip
版本venv
?
How do I control the version of pip
which is used in a freshly created venv
?
默认情况下,它使用供应的点数分布可能已经过时或由于其他任何原因而不合适.我希望能够创建一个venv
,并且首先安装 并安装用户指定的pip版本,而不是先创建一个,然后再从环境中升级pip安装.
By default, it uses a vendored pip distribution which may be out of date or unsuitable for whatever other reason. I want to be able to create a venv
with a user-specified version of pip installed initially, as opposed to creating one and then upgrading the pip installation from within the env.
推荐答案
stdlib的新添加 venv
命令行界面.
A new addition to the stdlib venv
module is EnvBuilder.upgrade_dependencies
. However, it didn't make it into the venv
command-line interface in time for the 3.8.0 release.
不幸的是,它不会真正帮助用户安装特定于 的pip版本,只是最新版本.
Unfortunately, it won't really help users to install a specific pip version, only the latest.
一切都不会丢失! venv
CLI确实提供了一个--without-pip
参数,在这里很有用.您可以使用此选项退出供应商的pip,然后实际使用供应商的pip wheel来安装所需的pip版本(以及在刚创建的虚拟环境中可能需要的任何其他软件包).这有点令人费解,因此最好将其放入shell函数中-这将进入您的shell配置文件或rc文件中:
All is not lost! The venv
CLI does provide a --without-pip
argument that is useful here. You can use this to opt-out of the vendored pip, and then actually use the vendored pip wheel to install your desired pip version instead (along with any other packages you might want in a freshly created virtual environment). This is all a bit convoluted, so best to put it into a shell function - this goes into your shell profile or rc file:
function ve() {
local py="python3"
if [ ! -d ./.venv ]; then
echo "creating venv..."
if ! $py -m venv .venv --prompt=$(basename $PWD) --without-pip; then
echo "ERROR: Problem creating venv" >&2
return 1
else
local whl=$($py -c "import pathlib, ensurepip; [whl] = pathlib.Path(ensurepip.__path__[0]).glob('_bundled/pip*.whl'); print(whl)")
echo "boostrapping pip using $whl"
.venv/bin/python $whl/pip install --upgrade pip setuptools wheel
source .venv/bin/activate
fi
else
source .venv/bin/activate
fi
}
如前所述,此函数仅从索引中提取最新的pip
,setuptools
和wheel
.要强制使用特定版本,您只需更改以下shell脚本行即可:
As written, this function just pulls latest pip
, setuptools
, and wheel
from index. To force specific versions you can just change this line of the shell script:
.venv/bin/python $whl/pip install --upgrade pip setuptools wheel
例如,
.venv/bin/python $whl/pip install pip==19.3.1
对于Python 2.7用户,您可能会做类似的技巧,因为virtualenv
在--no-pip
,--no-setuptools
和--no-wheel
中提供了类似的命令行选项,并且仍然有一个供应商提供的pip wheel用于引导自Python 2.7.9起. Pathlib将不可用,因此您需要将pathlib
用法更改为os.path
+
For Python 2.7 users, you may do a similar trick because virtualenv
provides similar command-line options in --no-pip
, --no-setuptools
, and --no-wheel
, and there is still a vendored pip wheel available to bootstrap since Python 2.7.9. Pathlib will not be available, so you'll need to change the pathlib
usage into os.path
+ glob
.
这篇关于在virtualenv中控制pip版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!