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

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

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


当前回答

在Linux、Mac OS X和其他POSIX系统上,使用带版本控制的Python命令和-m开关来运行pip的适当副本:

python2.7 -m pip install SomePackage
python3.4 -m pip install SomePackage

(也可以使用版本适当的PIP命令)

在Windows上,将py Python启动器与-m开关结合使用:

py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3.4 -m pip install SomePackage  # specifically Python 3.4

如果py -3.4有错误,那么试试:

pip install SomePackage

其他回答

您可以使用以下命令之一:

pip2 install SomePackage
pip3 install SomePackage

python2 -m pip install SomePackage  
python3 -m pip install SomePackage 

当然,还要确保安装了正确的pip版本

sudo apt-get install python-pip
sudo apt-get install python3-pip

我自己没有使用过这些命令,但是上面的一些回答建议使用它们来指定你想使用的python版本

pip-2.7 install SomePackage
python-3.6 -m pip install SomePackage

网址:https://docs.python.org/3/installing/

下面是如何为同时安装的linux, mac, posix的不同版本安装包:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4
python3.5 -m pip install SomePackage  # specifically Python 3.5
python3.6 -m pip install SomePackage  # specifically Python 3.6

在Windows上,将py Python启动器与-m开关结合使用:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

PIP也是一个python包。因此,将模块安装到特定python版本的最简单方法如下所示

 python2.7 /usr/bin/pip install foo

or

python2.7 -m pip install foo

pip debug命令提供了一些用于调试的有用信息。它在第一行(警告之后)显示了附加的python解释器的位置。

$ pip debug
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 21.2.4 from /data/akshay/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
sys.version: 3.9.12 (main, Apr  5 2022, 06:56:58)
sys.executable: /data/akshay/anaconda3/bin/python 

我自己最近也遇到了这个问题,发现在我的Linux系统上,Python 2也没有得到正确的Python 3的pip。

首先,你必须确保已经为你的python版本安装了pip:

对于Python 2:

sudo apt-get install python-pip

对于Python 3:

sudo apt-get install python3-pip

然后,要安装一个版本的Python或其他版本的包,只需在Python 2中使用以下命令:

pip install <package>

或Python 3:

pip3 install <package>