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

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

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


当前回答

显然,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
>>> 

其他回答

搅拌机:

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

简单而近期

在windows上

1-假设您的系统中安装了不同版本的Python。使用以下命令进行检查:

> py --list
 -3.10-64 *
 -3.7-64

2-设置您首选的默认版本:

通过设置PY_PYTHON环境变量(例如PY_PYTHON=3.7)。 通过设置py.ini文件通常位于C:\Users\<您的用户名>\AppData\Local如果没有创建一个。例如,设置PY_PYTHON=3和PY_PYTHON3=3.7环境变量等价于INI文件包含:

[defaults]
python=3
python3=3.7

3-检查再次输入py——list:

> py --list
 -3.10-64 
 -3.7-64 *

4-如果你想用特定版本的python和pip运行虚拟环境,请参阅这篇文章。

Linux (Ubuntu)

在Linux上管理不同的python版本的一个简单方法是update-alternatives命令。这个命令使我们能够轻松地在同一软件的多个版本之间切换。

command format:update-alternatives --install link name path priority, the name is the generic name for the master link, the link is the name of its symlink, the path is the alternative being introduced for the master link, and priority is the priority of the alternatives group. Usage: Suppose you installed two versions of python (python3.10 , python3.7). Now by running this command you will link the command name (python3) to different versions of python and assign a priority number. A higher priority number means a higher priority.

$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2

使用此命令列出已安装的python版本:

$ update-alternatives --list python3
/usr/bin/python3.7
/usr/bin/python3.10

版本切换:执行以下命令后,只需手动选择所需python版本的优先级编号。

$ update-alternatives --config python3

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                      Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.10   2         auto mode
* 1            /usr/bin/python3.7   1         manual mode
  2            /usr/bin/python3.10   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

例如,您可以转到C:\Python2.7\Scripts,然后从该路径运行cmd。然后你可以运行pip2.7安装你的包…

这将为该版本的Python安装包。

具体到windows: \path\to\python.exe -m pip install PackageName works

在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