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

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

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


当前回答

网址: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

其他回答

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

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

目前建议使用python -m pip,其中python是您想使用的python版本。这是推荐的,因为它适用于所有版本的Python和所有形式的virtualenv。例如:

# The system default python:
$ python -m pip install fish

# A virtualenv's python:
$ .env/bin/python -m pip install fish

# A specific version of python:
$ python-3.6 -m pip install fish

之前的答案,留给后人:

从0.8版本开始,Pip支持Pip -{version}。你可以像easy_install-{version}一样使用它:

$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage

编辑:在1.5版中,pip将其模式更改为使用pipVERSION而不是pip- version。如果你的pip >= 1.5,你应该使用以下命令:

$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

更多详情请登录https://github.com/pypa/pip/pull/1053


引用:

https://github.com/pypa/pip/issues/200 http://www.pip-installer.org/docs/pip/en/0.8.3/news.html#id4 https://pip.pypa.io/en/stable/news/ v0-8或 https://web.archive.org/web/20140310013920/http://www.pip-installer.org:80/docs/pip/en/0.8.3/news.html#id4

对于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正常命令

这可能是完全错误的做法(我是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())

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

 python2.7 /usr/bin/pip install foo

or

python2.7 -m pip install foo