处理多个 Python 版本和 PIP?
有什么方法可以让pip
与多个版本的Python兼容?例如,我想使用 pip
将内容显式安装到我的站点 2.5 安装或我的站点 2.6 安装。
例如,对于 easy_install
,我使用 easy_install-2.{5,6}
。
而且,是的 - 我知道 virtualenv,但不 - 它不是这个特定问题的解决方案。
Is there any way to make pip
play well with multiple versions of Python? For example, I want to use pip
to explicitly install things to either my site 2.5 installation or my site 2.6 installation.
For example, with easy_install
, I use easy_install-2.{5,6}
.
And, yes — I know about virtualenv, and no — it's not a solution to this particular problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
pip 也是一个 python 包。因此,将模块安装到特定 python 版本的最简单方法如下
或
pip is also a python package. So the easiest way to install modules to a specific python version would be below
or
显然,
easy_install
和pip
有多个版本。看来是大乱子了。无论如何,这就是我在 Ubuntu 12.10 上安装 Django for Python 2.7 的方法:So apparently there are multiple versions of
easy_install
andpip
. It seems to be a big mess. Anyway, this is what I did to install Django for Python 2.7 on Ubuntu 12.10:在 Linux、Mac OS X 和其他 POSIX 系统上,将版本化的 Python 命令与
-m
开关结合使用来运行pip
的相应副本:(适当版本化的 pip 命令也可能可用)
在 Windows 上,将
py
Python 启动器与-m
开关结合使用:如果您收到
py -3.4
然后尝试:On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the
-m
switch to run the appropriate copy ofpip
:(appropriately versioned pip commands may also be available)
On Windows, use the
py
Python launcher in combination with the-m
switch:if you get an error for
py -3.4
then try:安装多个版本的 Python 和各自的软件包。
同一 Windows 计算机上的 Python 版本:2.7、3.4 和 3.6
安装所有 3 个版本的 Python:
所有 3 个版本的 Python 的 PATH :
重命名版本的可执行文件:
检查所有版本的命令提示符:
为每个版本单独安装软件包
Installation of multiple versions of Python and respective Packages.
Python version on the same windows machine : 2.7 , 3.4 and 3.6
Installation of all 3 versions of Python :
PATH for all 3 versions of Python :
Renaming the executables for versions :
Checked for the command prompt with all versions :
Installing the packages separately for each version
如果您有多个版本以及多个体系结构(32 位、64 位),则需要在版本末尾添加 -32 或 -64。
对于 Windows,请转到 cmd 并输入 py --list,它将生成您已安装的版本。该列表将如下所示:
作为示例的完整命令将是:
如果您想更深入地了解,要在特定版本的 python 上安装特定版本的包,请在包后面使用 ==(version)。举个例子,
If you have multiple versions as well as multiple architectures (32 bit, 64 bit) you will need to add a -32 or -64 at the end of your version.
For windows, go to cmd and type py --list and it will produce the versions you have installed. The list will look like the following:
The full command as an example will be:
If you want to get more indepth, to install a specific version of a package on a specific version of python, use ==(version) after the package. As an example,
这是我对这个问题的看法。适用于 Python3。主要功能包括:
先决条件: 如果您使用的是一些没有额外功能的精简瘦客户端安装 turf 后,您应该首先运行它(至少在 ubuntu 18.04 中,为方便起见添加了额外的软件包):
步骤如下:
如果您以其他方式安装了多个额外的 python 版本,请删除它们,例如,删除 $HOME/.local/lib/python3.x 等(也是全局安装的)。不过,请勿触及系统的默认 python3 版本。
在以下目录结构下下载不同Python版本的源代码:
<前><代码> $HOME/
python_versions/ :在此处下载 Python-*.tgz 包并“tar xvf”它们。你会得到这样的目录:
Python-3.4.8/
Python-3.6.5/
Python-3.xy/
...
在每个“Python-3.xy/”目录中,执行以下操作(请勿在任何步骤中使用“sudo”! ):
在“python_versions/”中创建如下文件:
<前><代码> env_python3x.bash:
#!/bin/bash
echo“输入 deactivate 退出”
源 $HOME/python_versions/Python-3.xy/env/bin/activate
现在,无论何时您希望选择 python3.x,都可以这样做
可以输入 virtualenv
在 virtualenv 中,安装你最喜欢的 python 包
要退出 virtualenv 和 python 版本,只需键入“deactivate”
UPDATE
似乎
--no-site-packages
已被弃用。有一个简单的修复方法:激活 virtualenv 后,只需将 HOME env 变量指向实际主目录之外的其他位置,即:一般来说,执行此操作的一个好方法是:
ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/
export PYTHONPATH=< /code>,
export HOME=/some/other/dir
现在您应该拥有自定义隔离的 virtualenv。
更新 2 / SUDO
不想强制 sudo 使用你的 virtualenv 吗?
现在尝试“sudo python3 --version”,神奇的事情就会发生
UPDATE 3 / DOCKER
在你的 docker 中启用 virtualenv (当然,你已经在你的 docker 镜像中构建了它):
Here is my take on the problem. Works for Python3. The main features are:
Prerequisites: If you are using some bare-bones thin client with no extra turf installed, you should run this first (in ubuntu 18.04 at least, extra packages added for convenience):
The steps are as follows:
If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though.
Download source for different python versions under the following directory structure:
At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!):
At "python_versions/" create files like this:
Now, anytime you wish to opt for python3.x, do
to enter the virtualenv
While in the virtualenv, install your favorite python packages with
To exit the virtualenv and python version just type "deactivate"
UPDATE
It seems that
--no-site-packages
is deprecated. There's an easy fix for this: Once you have activated the virtualenv, just point the HOME env variable to somewhere else than your actual home directory, i.e.:A nice way to do this in general is:
ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/
export PYTHONPATH=
,export HOME=/some/other/dir
Now you should have custom-isolated virtualenv.
UPDATE 2 / SUDO
Wan't to force sudo to use your virtualenv?
Now try "sudo python3 --version" and magic should happen
UPDATE 3 / DOCKER
Enable virtualenv inside your docker (of course, you have built it in your docker image):
您可以使用以下命令之一:
当然,请确保您安装了正确版本的 pip
我自己没有使用过这些命令,但是上面的一些答案建议使用它们来准确指定您要使用的 python 版本
You can use one of the following commands:
And of course, make sure that you have the correct version of pip installed
I haven't used these commands myself but, some answers above suggests using them to specify exactly the version of python you want to use
对于 python 3 和 Windows 操作系统,我总是使用此语法在不同版本上安装包:
首先,我总是使用 Git Bash 命令提示符。
这是安装 urllib 包的示例。
默认Python版本:(普通pip命令)
对于其他版本
For python 3 and Windows OS, I always use this syntax to install packages on different version:
First I always use Git Bash Command Prompt.
Here an example installing urllib package.
Default Python version:(The normal pip command)
For the other versions
从 pip
22.3
,您可以使用--python
选项允许pip
管理除安装 pip 之外的 Python 环境。引用文档中的:
Starting from pip
22.3
, you can use--python
option to allowpip
to manage Python environments other than the one pip is installed in.Quoting from the documentation:
这里的大多数答案都解决了这个问题,但我想添加一些关于在 CentOS 7 上的
/usr/local
中创建 python 替代安装时一直让我困惑的东西。当我在那里安装时,看起来 pip 正在工作,因为我可以使用pip2.7 install
并且它会安装模块。然而,我不明白的是为什么我新安装的 python 版本看不到我正在安装的内容。原来在CentOS 7中,
/usr/bin
文件夹下已经有一个python2.7和一个pip2.7。要为新的 python 发行版安装 pip,您需要明确告诉 sudo 转到/usr/local/bin
这应该将 pip2.7 安装在您的
/usr/local/bin< /code> 文件夹以及您的 python 版本。诀窍是,当你想安装模块时,你要么需要修改 sudo
$PATH
变量以包含/usr/local/bin
,要么需要执行想要安装新模块。我花了很长时间才想起
sudo
并没有立即看到/usr/local/bin
。Most of the answers here address the issue but I want to add something what was continually confusing me with regard to creating an alternate installation of python in the
/usr/local
on CentOS 7. When I installed there, it appeared like pip was working since I could usepip2.7 install
and it would install modules. However, what I couldn't figure out was why my newly installed version of python wasn't seeing what I was installing.It turns out in CentOS 7 that there is already a python2.7 and a pip2.7 in the
/usr/bin
folder. To install pip for your new python distribution, you need to specifically tell sudo to go to/usr/local/bin
This should get pip2.7 installed in your
/usr/local/bin
folder along with your version of python. The trick is that when you want to install modules, you either need to modify the sudo$PATH
variable to include/usr/local/bin
or you need to executeif you want to install a new module. It took me forever to remember that
sudo
wasn't immediately seeing/usr/local/bin
.上下文:Archlinux
操作:
安装 python2-pip:
sudo pacman -S python2-pip
您现在拥有 pip2.7:
sudo pip2.7 install boto
测试(在我的例子中,我需要“boto”):
运行以下命令:
成功:没有错误。
退出:Ctrl+D
Context: Archlinux
Action:
Install python2-pip:
sudo pacman -S python2-pip
You now have pip2.7:
sudo pip2.7 install boto
Test (in my case I needed 'boto'):
Run the following commands:
Success: No error.
Exit: Ctrl+D
例如,如果您将其他版本(例如 3.5)设置为默认值并希望为 python 2.7 安装 pip:请
for example, if you set other versions (e.g. 3.5) as default and want to install pip for python 2.7:
如果您同时安装了
python3.6
和python3.7
并且希望将pip
与python3.7
一起使用默认情况下,您应该执行以下操作:首先确保您已为
python3.7
安装了pip
现在
pip3.7
必须可用,因此我们编辑.bashrc
添加以下行为
了使更改生效,请在 shell 中输入:
现在,如果您输入:
您应该得到:
这意味着,如果您使用,例如:
它将安装
对于python3.7
If you have both
python3.6
andpython3.7
installed and want to usepip
withpython3.7
by default, here's what you should do:First make sure you have
pip
installed forpython3.7
Now
pip3.7
must be available, so we edit.bashrc
adding the following line to it
In order for the changes to take effect type in the shell:
Now if you type:
you should get:
which means, if you use, for example:
it would install the
<package>
forpython3.7
另一种可能的方法是使用 conda 和 pip。有时您可能只想使用其中之一,但如果您确实需要设置特定版本的 python,我会将两者结合起来。
我用我想要的 python 创建了一个起始 conda 环境。就像这里 https://docs .conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html。或者,您可以仅使用 conda 设置整个环境。
conda create -n myenv python=3.6.4
然后使用您喜欢的 python 激活您的环境。此命令可能会根据操作系统而变化。
源 activae myenv
现在你的 python 已处于活动状态,那么你可以继续使用 conda,但如果你需要/想要使用 pip:
python -m pip -r requests.txt
这里你有一个可能的方法。
Another possible way could be using conda and pip. Some time you probably want to use just one of those, but if you really need to set up a particular version of python I combine both.
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
Here you have a possible way.
要使用多个版本的 pip,只需键入
示例:对于 python 3.10
pip3.10
适用于 Python 3.7
To use multiple versions of pip, just type
Example: for python 3.10
pip3.10
for Python 3.7
在使用 pip 的特定位置时更新 PIP 并安装软件包:
在某些设备上使用简单的命令行
pip install
时,我收到间歇性错误“访问被拒绝”所有用户使用c:\users\...
文件夹而不是c:\Program Files\Python
文件夹中的 pip 的实验室计算机。感谢上面的 MultiplyByZer0 答案,我能够实现一个指定 python 路径然后运行 pip 的解决方案。
这是我用来强制 pip 更新然后安装 python 库的 powershell 脚本:
Updating PIP and installing Packages while using specific location of pip:
I was getting an intermittent error "Access Denied" when using the simple comand line
pip install <library>
on some lab machines that were using pip from ac:\users\...
folder instead of thec:\Program Files\Python
folder for all users.Thanks to MultiplyByZer0 answer above I was able to implement a solution that specified the path to python and then run pip.
Here is the powershell script I use to force pip update and then install python libraries:
根据最新的 Ubuntu-LTS 22.04 和 python 3 进行更新,但对于 python 2 可以遵循类似的说明并进行适当的版本更改。
您可以考虑使用 pyenv python 环境管理器,它可以让您并行配置不同版本的 python,而无需创建单独的 virtualenv 或需要安装单独的 pip。
安装说明可以在这里找到,配置 pyenv 非常容易。 https://github.com/pyenv/pyenv#readme
但是,这确实需要您安装可以按如下方式安装必备库:
基于 Redhat 的系统
基于 Ubuntu/Debian 的 linux 系统
安装必备库后,请按照 README.md 中的说明进行操作
安装完成后,要检查系统中存在的 python 版本,请在终端中运行以下命令。
注意,如果您在设置 pyenv 之前安装了 python 版本,它可能无法检测到已安装的版本,您可能必须删除它们(包括依赖项)。执行此操作时请注意不要删除重要的 lib 文件。
安装新版本的 python
安装后将 python 版本设置为默认版本,运行以下命令。
您可以随时使用
pyenv global
、pyenv local
或pyenv shell
命令以及所需的版本号在已安装的 Python 版本之间进行切换。请注意,这种安排与虚拟环境不同,因为它允许您在使用其他系统级库和包的同时在系统范围内管理多个版本的 python。然而 pyenv 确实有插件来启用 virtualenv 支持,可以按照此处列出的说明进行安装 https://github .com/pyenv/pyenv-virtualenv#readme
只需删除 pyenv 文件夹并恢复对
.bashrc
、.bash_profile
和/或类似的文件。希望这有帮助。
Updated according to latest Ubuntu-LTS 22.04 and for python 3, however similar instructions can be followed for python 2 with appropriate version changes.
You can consider pyenv python environment manager which lets you configure different versions of python parallely without needing to create a separate virtualenv or requiring separate pip installed.
The installation instructions can be found here and it is pretty easy to configure pyenv. https://github.com/pyenv/pyenv#readme
This does however require you to install prerequisite libraries which can be installed as follows:
Redhat based systems
Ubuntu/Debian based linux systems
After installing prerequisites follow the instructions in the README.md
Once installation is finished, to check python versions present in your system run the following in your terminal
Note if you have installed python versions before setting up pyenv it may not detect the installed versions and you might have to remove them including dependencies. Take care to not remove important lib files while doing so.
To install a new version of python
Once installed to set the python version as default run the following.
You can switch between installed Python versions at any time using the
pyenv global
,pyenv local
, orpyenv shell
commands with the desired version number.Note that this arrangement is different from a virtual environment as it allows you to manage multiple versions of python at a system-wide level while using other system level libraries and packages. However pyenv does have plugins to enable virtualenv support which can be installed from instructions listed here https://github.com/pyenv/pyenv-virtualenv#readme
Pyenv can be removed by simply deleting the pyenv folder and reverting any changes made to
.bashrc
,.bash_profile
and/or similar files.Hope this helps.
例如,您可以转到 C:\Python2.7\Scripts,然后从该路径运行 cmd。之后你可以运行 pip2.7 install yourpackage...
这将为该版本的 Python 安装包。
You can go to for example C:\Python2.7\Scripts and then run cmd from that path. After that you can run pip2.7 install yourpackage...
That will install package for that version of Python.
这可能是完全错误的做法(我是一个 python 菜鸟),但我只是进去编辑了 pip 文件
This is probably the completely wrong thing to do (I'm a python noob), but I just went in and edited the pip file
特别是对于 Windows:
\path\to\python.exe -m pip install PackageName 有效。
For windows specifically:
\path\to\python.exe -m pip install PackageName works.
对于搅拌机:
for Blender:
当前建议是使用
python -m pip
,其中python
是您要使用的 Python 版本。这是建议,因为它适用于所有版本的 Python 和所有形式的 virtualenv。例如:之前的答案,留给后人:
从0.8版本开始,Pip支持
pip-{version}
。您可以像easy_install-{version}
一样使用它:编辑:pip 更改了其架构以使用
pipVERSION
而不是pip-版本 1.5 中的版本。如果您有
pip >= 1.5
,您应该使用以下内容:检查 https ://github.com/pypa/pip/pull/1053 了解更多详细信息
参考:
http://www.pip-installer.org/docs/pip/en/0.8.3/news.html#id4< /s>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
The current recommendation is to use
python -m pip
, wherepython
is the version of Python you would like to use. This is the recommendation because it works across all versions of Python, and in all forms of virtualenv. For example:Previous answer, left for posterity:
Since version 0.8, Pip supports
pip-{version}
. You can use it the same aseasy_install-{version}
:EDIT: pip changed its schema to use
pipVERSION
instead ofpip-VERSION
in version 1.5. You should use the following if you havepip >= 1.5
:Check https://github.com/pypa/pip/pull/1053 for more details
References:
http://www.pip-installer.org/docs/pip/en/0.8.3/news.html#id4https://pip.pypa.io/en/stable/news/#v0-8 or
https://web.archive.org/web/20140310013920/http://www.pip-installer.org:80/docs/pip/en/0.8.3/news.html#id4
在 Windows 上,您可以通过 Python 启动器使用给定的 Python 版本执行 pip 模块、
py.exe
(如果您在 Python 3 安装过程中选择安装它)。您可以更具体并请求准确的 Python 子版本:
要通过启动器获取可用的所有已安装 Python 版本的列表,请运行:
或者,您可以直接启动所需的 Python 可执行文件:
On Windows, you can execute the pip module using a given Python version through the Python launcher,
py.exe
, if you chose to install it during Python 3 setup.You can be even more specific and request an exact sub-version of Python:
To get a list of all installed Python versions available through the launcher, run:
Alternatively, you can launch the desired Python executable directly:
/path/to/python2.{5,6} /path/to/pip install PackageName
不起作用?要使其在尚未安装 pip 的任何 python 版本上运行,您需要下载 pip 并执行
python*version* setup.py install
。例如python3.3 setup.py install
。这解决了注释中的导入错误。 (根据@hbdgaf的建议)/path/to/python2.{5,6} /path/to/pip install PackageName
doesn't work?For this to work on any python version that doesn't have pip already installed you need to download pip and do
python*version* setup.py install
. For examplepython3.3 setup.py install
. This resolves the import error in the comments. (As suggested by @hbdgaf)我默认安装了 python 2.6 (Amazon EC2 AMI),但我的应用程序需要 python2.7 和一些外部包。假设你已经安装了 python2.7 和默认的 python(在我的例子中是 2.6)。以下是如何为非默认 python2.7 安装 pip 和软件包
为您的 python 版本安装 pip:
使用特定的 pip 版本安装软件包:
I had python 2.6 installed by default (Amazon EC2 AMI), but needed python2.7 plus some external packages for my application. Assuming you already installed python2.7 alongside with default python (2.6 in my case). Here is how to install pip and packages for non-default python2.7
Install pip for your python version:
Use specific pip version to install packages:
它在 Windows 中以这种方式为我工作:
我将 python 文件 python.py 和 pythonw.exe 的名称更改为 python3.py pythonw3.py
然后我只是在提示符中运行此命令:
python3 -m pip 安装包
It worked for me in windows this way:
I changed the name of python files python.py and pythonw.exe to python3.py pythonw3.py
Then I just ran this command in the prompt:
python3 -m pip install package
其他答案显示了如何在 2.X 和 3.X Python 中使用 pip,但没有显示如何处理多个 Python 发行版(例如原始 Python 和 Anaconda Python)的情况。
我总共有3个Python版本:原始Python 2.7和Python 3.5以及Anaconda Python 3.5。
以下是我如何将包安装到:
原始Python 3.5:
原始Python 2.7:
Anaconda Python 3.5:
或
更简单,因为 Anaconda 会覆盖用户环境中的原始 Python 二进制文件。
当然,在anaconda中安装应该使用
conda
命令完成,这只是一个示例。另外,请确保为该特定 python 安装了 pip。您可能需要手动安装 pip。这适用于 Ubuntu 16.04:
或
Other answers show how to use pip with both 2.X and 3.X Python, but does not show how to handle the case of multiple Python distributions (eg. original Python and Anaconda Python).
I have a total of 3 Python versions: original Python 2.7 and Python 3.5 and Anaconda Python 3.5.
Here is how I install a package into:
Original Python 3.5:
Original Python 2.7:
Anaconda Python 3.5:
or
Simpler, as Anaconda overrides original Python binaries in user environment.
Of course, installing in anaconda should be done with
conda
command, this is just an example.Also, make sure that pip is installed for that specific python.You might need to manually install pip. This works in Ubuntu 16.04:
or
从这里: https://docs.python.org/3/installing/
下面是方法要安装同时安装的各种版本的软件包 linux、mac、posix:
在 Windows 上,将 py Python 启动器与 -m 开关结合使用:
From here: https://docs.python.org/3/installing/
Here is how to install packages for various versions that are installed at the same time linux, mac, posix:
On Windows, use the py Python launcher in combination with the -m switch:
我最近遇到了这个问题,发现我在也有 Python 2 的 Linux 系统上没有获得适用于 Python 3 的正确 pip。
首先,您必须确保已安装适用于您的 python 版本的 pip:
对于 Python 2:
对于 Python 3:
然后要安装一个版本的 Python 或另一个版本的软件包,只需对 Python 2 使用以下命令:
或对 Python 3 使用以下命令:
I ran into this issue myself recently and found that I wasn't getting the right pip for Python 3, on my Linux system that also has Python 2.
First you must ensure that you have installed pip for your python version:
For Python 2:
For Python 3:
Then to install packages for one version of Python or the other, simply use the following for Python 2:
or for Python 3: