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

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

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


当前回答

简单而近期

在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

其他回答

其他答案展示了如何将pip与两者一起使用。X和3。X Python,但没有说明如何处理多个Python发行版的情况(例如。原始Python和Anaconda Python)。

我总共有3个Python版本:原始Python 2.7和Python 3.5和Anaconda Python 3.5。

下面是我如何安装一个包到:

原始Python 3.5: /usr/bin/python3 -m PIP安装python-daemon 原始Python 2.7: /usr/bin/python -m PIP安装python-daemon 蟒蛇3.5: Python3 -m PIP安装python-daemon 或 Pip3安装python-daemon 更简单,因为Anaconda在用户环境中覆盖原始的Python二进制文件。 当然,安装在anaconda应该用conda命令来完成,这只是一个例子。


另外,确保为特定的python安装了pip。您可能需要手动安装pip。这适用于Ubuntu 16.04:

sudo apt-get install python-pip 

or

sudo apt-get install python3-pip

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

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

简单而近期

在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

/ / python2 /路径。{5,6} /path/to/pip install PackageName不正常?

为了在任何没有安装pip的python版本上工作,你需要下载pip并执行python*version* setup.py install。例如python3.3 setup.py install。这解决了注释中的导入错误。(@hbdgaf建议)

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