[英]How to use pip with python 2 & 3 installed? (OSX)
I am trying to get python 3 working on my OSX laptop.我正在尝试让 python 3 在我的 OSX 笔记本电脑上工作。
I need to install requests for python 3, and it isn't working.我需要为 python 3 安装请求,但它不起作用。
I think I've managed to get pip installed for both python 2.7 & python 3 however...我想我已经设法为 python 2.7 和 python 3 安装了 pip 但是......
Whenever I use 'pip' it points to python2... I can't seem to access the pip for python 3?每当我使用“pip”时,它都指向 python2...我似乎无法访问 python 3 的 pip?
In all likelihood, pip3
will be installed pointing to your Python 3 installation, so your use case is probably solvable by just switching from:很有可能, pip3
将安装指向您的 Python 3 安装,因此您的用例可能只需从以下位置切换即可解决:
$ pip install foo
to:到:
$ pip3 install foo # Or pip3.7 install foo if you need to disambiguate further
That said, it can get kind of complicated when you have many different Python installs, where pip
/ pip3
might have been installed pointing to a Python version that doesn't correspond to the python
/ python3
you're using, which can be quite confusing.也就是说,当您安装了许多不同的 Python 时,它可能会变得有点复杂,其中pip
/ pip3
可能已安装指向与您正在使用的python
/ python3
不对应的 Python 版本,这可能会非常混乱.
If you know python
& python3
are the correct executable, just use it to invoke pip
on your behalf.如果您知道python
和python3
是正确的可执行文件,只需使用它代表您调用pip
。 It's fairly easy too, just check your version to be sure it's the one you expect (eg on my system):这也很容易,只需检查您的版本以确保它是您期望的版本(例如在我的系统上):
$ python --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.6
then use the appropriate one with -mpip
, a flag to run an installed module/package via the chosen Python as the "main" executable, bypassing the need for specifically versioned pip
executable entirely.然后使用适当的-mpip
标志,通过选择的 Python 作为“主”可执行文件运行已安装的模块/包,完全绕过了对特定版本的pip
可执行文件的需要。 So if you wanted to install foo
for Python 3.6 on my machine, you'd run:因此,如果您想在我的机器上为 Python 3.6 安装foo
,您可以运行:
$ python3 -mpip install foo
This is especially useful on Windows, where the pip
executables often either don't exist, or are not installed in the PATH
, so it's irritating to use them.这在 Windows 上特别有用,因为pip
可执行文件通常不存在,或者没有安装在PATH
,所以使用它们很烦人。 Instead, use the Windows launcher that comes with any modern Python 3 version (but manages all Python versions on the machine), and is used to disambiguate among various versions.相反,使用任何现代 Python 3 版本附带的Windows 启动器(但管理机器上的所有 Python 版本),并用于消除各种版本之间的歧义。 For example:例如:
C:\>; Installs foo for latest installed version of Python 3
C:\>py -3 -mpip install foo
C:\>; Installs foo for latest installed version of Python 2
C:\>py -2 -mpip install foo
C:\>; Installs foo for latest installed version of Python 3.6
C:\>py -3.6 -mpip install foo
Essentially, any use of pip
can be replaced by executing the Python interpreter directly with the -mpip
option to run the pip
package as the "main" executable.本质上,任何pip
使用都可以通过使用-mpip
选项直接执行 Python 解释器来替换,以将pip
包作为“主”可执行文件运行。
This trick applies to many other tools with dedicated launchers that are often not installed in the PATH
, particularly on Windows, and it makes updates easier too;这个技巧适用于许多其他带有专用启动器的工具,这些工具通常没有安装在PATH
,特别是在 Windows 上,它也使更新更容易; my Windows shortcut for launching ipython3
never used a hardcoded path to the launcher (eg C:\\Program Files\\Python36\\Scripts\\ipython3.exe
), instead using %WINDIR%\\py.exe -3 -mIPython
.我用于启动ipython3
Windows 快捷方式从未使用启动器的硬编码路径(例如C:\\Program Files\\Python36\\Scripts\\ipython3.exe
),而是使用%WINDIR%\\py.exe -3 -mIPython
。 In addition to being more portable (the shortcut "just works" on any Windows system with a semi-recent Python 3 install), it's self-updating;除了更便携(快捷方式在任何安装了半最新 Python 3 的 Windows 系统上都“有效”),它是自我更新的; when I upgraded from 3.6 to 3.7, the shortcut didn't have to change at all (I had to run py -3 -mpip install ipython
again to get IPython reinstalled, but once I'd done that, the shortcut seamlessly began referring to the 3.7 install with no changes needed).当我从 3.6 升级到 3.7 时,快捷方式根本没有改变(我不得不再次运行py -3 -mpip install ipython
来重新安装 IPython,但是一旦我这样做了,快捷方式就无缝地开始引用3.7 安装,无需更改)。
Run this command to find the python that is used before running pip: which python
.运行此命令以查找运行 pip 之前使用的 python: which python
。 You can do the same idea to find which pip version is being run: which pip
您可以使用相同的想法来查找正在运行的 pip 版本: which pip
You'll need to create separate virtual environments in order to use different python versions and/or python dependencies.您需要创建单独的虚拟环境才能使用不同的 python 版本和/或 python 依赖项。 Use something like conda or venv to do this.使用 conda 或 venv 之类的东西来做到这一点。 Then, ensure that the desired python version virtual environment is activated prior to installing a new package with pip.然后,确保在使用 pip 安装新包之前激活所需的 python 版本虚拟环境。
To install requests
for python3
, use pip3 install requests
which is the pip
installer for Python 3 modules.要安装python3
requests
,请使用pip3 install requests
,它是 Python 3 模块的pip
安装程序。
This guide has some further info on getting Python 3 working on a mac.本指南提供了有关在 Mac 上运行 Python 3 的更多信息。 https://docs.python-guide.org/starting/install3/osx/ https://docs.python-guide.org/starting/install3/osx/
尝试先sudo apt-get update
然后sudo apt-get install python3-pip --fix-missing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.