有没有办法让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,不,它不是这个特殊问题的解决方案。
当前回答
简单而近期
在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
其他回答
在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
目前建议使用python -m pip,其中python是您想使用的python版本。这是推荐的,因为它适用于所有版本的Python和所有形式的virtualenv。例如:
# The system default python:
$ python -m pip install fish
# A virtualenv's python:
$ .env/bin/python -m pip install fish
# A specific version of python:
$ python-3.6 -m pip install fish
之前的答案,留给后人:
从0.8版本开始,Pip支持Pip -{version}。你可以像easy_install-{version}一样使用它:
$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage
编辑:在1.5版中,pip将其模式更改为使用pipVERSION而不是pip- version。如果你的pip >= 1.5,你应该使用以下命令:
$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage
更多详情请登录https://github.com/pypa/pip/pull/1053
引用:
https://github.com/pypa/pip/issues/200 http://www.pip-installer.org/docs/pip/en/0.8.3/news.html#id4 https://pip.pypa.io/en/stable/news/ v0-8或 https://web.archive.org/web/20140310013920/http://www.pip-installer.org:80/docs/pip/en/0.8.3/news.html#id4
安装多个版本的Python和相应的软件包。
同一台windows机器上的Python版本:2.7、3.4和3.6
安装所有3个版本的Python:
使用以下路径安装Python 2.7、3.4和3.6
Python所有3个版本的路径:
确保PATH变量(在系统变量中)包含以下路径:C:\Python27\;C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;
重命名可执行文件的版本:
将C:\Python36和C:\Python34中的python可执行文件名称分别更改为Python36和Python34。
检查所有版本的命令提示符:
分别为每个版本安装软件包
如果你同时安装了python3.6和python3.7,并且希望在默认情况下对python3.7使用pip,那么你应该这样做:
首先,确保为python3.7安装了pip
python3.7 -m pip install -U pip
现在pip3.7必须可用,因此我们编辑.bashrc
nano ~/.bashrc
向它添加下面的行
alias pip=pip3.7
为了使更改生效,在shell中输入:
source ~/.bashrc
现在如果你输入:
pip --version
你应该得到:
PIP 20.1.1 from /usr/local/lib/python3.7/dist-packages/ PIP (python 3.7)
这意味着,如果你使用,例如:
pip install <package>
它将为python3.7安装<package>
例如,如果您将其他版本(例如3.5)设置为默认版本,并希望为python 2.7安装PIP:
在https://pypi.python.org/pypi/pip (tar)下载PIP 解压缩tar文件 CD到文件的目录 Sudo python2.7 setup.py install