有没有办法让pip在多个版本的Python中都能很好地运行?例如,我想使用pip显式地将东西安装到我的站点2.5安装或站点2.6安装中。
例如,对于easy_install,我使用easy_install-2.{5,6}。
是的,我知道virtualenv,不,它不是这个特殊问题的解决方案。
有没有办法让pip在多个版本的Python中都能很好地运行?例如,我想使用pip显式地将东西安装到我的站点2.5安装或站点2.6安装中。
例如,对于easy_install,我使用easy_install-2.{5,6}。
是的,我知道virtualenv,不,它不是这个特殊问题的解决方案。
当前回答
我默认安装了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
其他回答
在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
这里有一种可能的方法。
对于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正常命令
简单而近期
在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 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