有没有办法让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

其他回答

它在windows中是这样工作的:

我将python文件的名称python.py和pythonw.exe更改为python3.py pythonw3.py 然后我在提示符中运行这个命令: Python3 -m 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

另一种可能的方法是使用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

这里有一种可能的方法。

上下文:Archlinux

行动: 安装python2-pip: sudo pacman -S python2-pip

你现在有了pip2.7: Sudo pip2.7安装boto

测试(在我的情况下,我需要'boto'): 执行如下命令:

python2
import boto

成功:没有错误。

退出:按Ctrl + D

我默认安装了python 2.6 (Amazon EC2 AMI),但我的应用程序需要python2.7加上一些外部包。假设您已经安装了python2.7和默认的python(在我的例子中是2.6)。下面是如何为非默认的python2.7安装pip和包

为你的python版本安装pip:

curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py

使用指定的pip版本安装软件包:

pip2.7 install mysql-connector-python --allow-external mysql-connector-python