有没有办法让pip在多个版本的Python中都能很好地运行?例如,我想使用pip显式地将东西安装到我的站点2.5安装或站点2.6安装中。

例如,对于easy_install,我使用easy_install-2.{5,6}。

是的,我知道virtualenv,不,它不是这个特殊问题的解决方案。


当前回答

对于python 3和Windows操作系统,我总是使用这种语法在不同版本上安装包:

首先,我总是使用Git Bash命令提示符。

这里有一个安装urllib包的例子。

默认Python版本:(普通pip命令)

pip install urllib3

对于其他版本

py -3.8 -m pip install urllib3

python的Py => -3.8 =>的版本(我使用3.8.7版本),但如果你使用3.7.7版本,它将是“-3.7” -m: just because or for modify PIP install urllib3: PIP正常命令

其他回答

在Windows上,如果您选择在Python 3设置期间安装pip模块,则可以通过Python启动器py.exe使用给定的Python版本执行pip模块。

py -3 -m pip install packagename
py -2 -m pip install packagename

你可以更具体地请求Python的精确子版本:

py -3.6 -m pip install packagename

要获得通过启动程序可用的所有已安装Python版本的列表,请运行:

py --list

或者,您可以直接启动所需的Python可执行文件:

C:/path/to/specific/python.exe -m pip install packagename

以下是我对这个问题的看法。适用于Python3。主要特点有:

每个Python版本都是从源代码编译的 所有版本都安装在本地 不会以任何方式破坏系统的默认Python安装 每个Python版本都使用virtualenv进行隔离

前提条件:如果你正在使用一些基本的瘦客户端,没有安装额外的turf,你应该首先运行这个(至少在ubuntu 18.04中,为了方便,额外的包添加了):

sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository universe
sudo apt-get update
sudo apt-get install -y build-essential cmake

sudo apt-get install -y zlib1g zlib1g-dev libsqlite3-dev \
openssl libssl-dev libffi-dev unzip pciutils net-tools \
libblas-dev gfortran libblas3 

具体步骤如下:

If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though. Download source for different python versions under the following directory structure: $HOME/ python_versions/ : download Python-*.tgz packages here and "tar xvf" them. You'll get directories like this: Python-3.4.8/ Python-3.6.5/ Python-3.x.y/ ... At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!): mkdir root ./configure --prefix=$PWD/root make -j 2 make install virtualenv --no-site-packages -p root/bin/python3.x env At "python_versions/" create files like this: env_python3x.bash: #!/bin/bash echo "type deactivate to exit" source $HOME/python_versions/Python-3.x.y/env/bin/activate Now, anytime you wish to opt for python3.x, do source $HOME/python_versions/env_python3x.bash

进入virtualenv

在virtualenv中,安装你喜欢的python包 PIP install——upgrade package_name 要退出virtualenv和python版本,只需输入“deactivate”

更新

似乎不赞成使用无站点包。有一个简单的解决方法:一旦你激活了virtualenv,只要将HOME env变量指向其他地方,而不是你实际的主目录,即:

export HOME=some/where/else

一般来说,一个很好的方法是:

创建virtualenv 激活virtualenv 如果你想“回收”现有的库到你的virtualenv,软链接它们从你现有的安装,即。 ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/ export PYTHONPATH=, export HOME=/some/other/dir

现在您应该有了自定义隔离的virtualenv。

更新2 / sudo

不想强迫sudo使用你的virtualenv吗?

Defaults        secure_path="/home/USENAME/Python-3.x.y/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults        env_keep += "VIRTUAL_ENV"
Defaults        env_keep += "PYTHONPATH"

现在尝试“sudo python3—version”,魔术应该会发生

更新3 / docker

在docker中启用virtualenv(当然,你已经在docker镜像中构建了它):

ENV VIRTUAL_ENV=/home/USER/Python-3.x.y/env
ENV PYTHONPATH=
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

另一种可能的方法是使用conda和pip。有时你可能只想使用其中的一个,但如果你真的需要设置一个特定版本的python,我会将两者结合起来。

I create a starting conda enviroment with the python I want. As in here https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html. Alternatively you could set up the whole enviroment just using conda. conda create -n myenv python=3.6.4 Then activate your enviroment with the python you like. This command could change depending on the OS. source activae myenv Now you have your python active then you could continue using conda but if you need/want to use pip: python -m pip -r requirements.txt

这里有一种可能的方法。

搅拌机:

/usr/bin $ python3.7 -m pip install irc

这可能是完全错误的做法(我是python新手),但我只是进入并编辑pip文件

#!/usr/bin/env python3 <-- I changed this line.

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())