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

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

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


当前回答

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

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

其他回答

例如,如果您将其他版本(例如3.5)设置为默认版本,并希望为python 2.7安装PIP:

在https://pypi.python.org/pypi/pip (tar)下载PIP 解压缩tar文件 CD到文件的目录 Sudo python2.7 setup.py install

这里的大多数答案都解决了这个问题,但我想补充一些一直让我困惑的东西,即在CentOS 7的/usr/local中创建python的替代安装。当我在那里安装时,它看起来像pip在工作,因为我可以使用pip2.7 install,它可以安装模块。然而,我不明白的是,为什么我新安装的python版本看不到我正在安装的东西。

在CentOS 7中,在/usr/bin文件夹中已经有一个python2.7和一个pip2.7。要为新的python发行版安装pip,您需要明确地告诉sudo转到/usr/local/bin

sudo /usr/local/bin/python2.7 -m ensurepip

这将使pip2.7与你的python版本一起安装在你的/usr/local/bin文件夹中。诀窍在于,当您想要安装模块时,您要么需要修改sudo $PATH变量以包含/usr/local/bin,要么需要执行

sudo /usr/local/bin/pip2.7 install <module>

如果您想安装一个新的模块。我花了很长时间才记住sudo没有立即看到/usr/local/bin。

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

这里有一种可能的方法。

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

显然,easy_install和pip有多个版本。看起来很乱。不管怎样,这是我在Ubuntu 12.10上安装Django for Python 2.7的方法:

$ sudo easy_install-2.7 pip
Searching for pip
Best match: pip 1.1
Adding pip 1.1 to easy-install.pth file
Installing pip-2.7 script to /usr/local/bin

Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo pip-2.7 install django
Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
  Running setup.py egg_info for package django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django
Cleaning up...

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>