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

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

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


当前回答

pip debug命令提供了一些用于调试的有用信息。它在第一行(警告之后)显示了附加的python解释器的位置。

$ pip debug
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 21.2.4 from /data/akshay/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
sys.version: 3.9.12 (main, Apr  5 2022, 06:56:58)
sys.executable: /data/akshay/anaconda3/bin/python 

其他回答

我默认安装了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

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

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

pip debug命令提供了一些用于调试的有用信息。它在第一行(警告之后)显示了附加的python解释器的位置。

$ pip debug
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 21.2.4 from /data/akshay/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
sys.version: 3.9.12 (main, Apr  5 2022, 06:56:58)
sys.executable: /data/akshay/anaconda3/bin/python 

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

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