如何将 pip 命令覆盖为 Python3.x 而不是 Python2.7?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38938205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to override the pip command to Python3.x instead of Python2.7?
提问by loki l
I am using OSX and I have pip installed for both Python3.5 and Python2.7. I know I can run the command pip2
to use Python2 and when I use the command pip3
Python3.x will be used.
The problem is that the default of pip
is set to Python2.7 and I want it to be Python3.x.
我正在使用 OSX,并且为 Python3.5 和 Python2.7 安装了 pip。我知道我可以运行命令pip2
来使用 Python2,当我使用命令pip3
时将使用 Python3.x。问题是默认pip
设置为Python2.7,我希望它是Python3.x。
How can I change that?
我怎样才能改变它?
edit: No, I am not running a virtual environment yet. If it was a virtual environment I could just run Python3.x and forget all about Python2.7, unfortunately since OSX requires Python2.7 for it's use I can't do that. Hence why I'm asking this.
编辑:不,我还没有运行虚拟环境。如果它是一个虚拟环境,我可以只运行 Python3.x 而忘记 Python2.7,不幸的是,因为 OSX 需要 Python2.7 才能使用我不能这样做。这就是为什么我要问这个。
Thanks for the answer. I however don't want to change what running python
does. Instead I would like to change the path that running pip
takes. At the moment pip -V
shows me pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7)
, but I am looking for pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5)
I am sure there has to be a way to do this. Any ideas?
谢谢你的回答。然而,我不想改变跑步的python
作用。相反,我想改变跑步的路径pip
。目前pip -V
显示给我pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7)
,但我正在寻找pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5)
我确信必须有办法做到这一点。有任何想法吗?
回答by Andrey Stukalenko
Run this:
运行这个:
pip3 install --upgrade --force pip
or even more explicit:
甚至更明确:
python3 -m pip install --upgrade --force pip
This will install pip for Python 3 and make Python 3 version of pip default.
这将为 Python 3 安装 pip 并使 Python 3 版本的 pip 默认。
Validate with:
验证:
pip -V
回答by Ghostkeeper
I always just run it via Python itself, this way:
我总是通过 Python 本身运行它,这样:
python3 -m pip install some_module
or
或者
python2 -m pip install some_module
The -m
calls the __main__.py
module of a specified package. Pip supports this.
该-m
调用__main__.py
指定包的模块。皮普支持这一点。
回答by Luke Taylor
Can't you alias pip='pip3'
in your ~/.bash_profile
?
你不能alias pip='pip3'
在你的~/.bash_profile
吗?
In Terminal, run nano ~/.bash_profile
, then add a line to the end that reads alias pip='pip3'
. This is safe; it won't affect system processes, only your terminal.
在终端中,运行nano ~/.bash_profile
,然后在末尾添加一行,内容为alias pip='pip3'
。这是安全的;它不会影响系统进程,只会影响您的终端。
回答by spectras
For your projects, you should be using a virtualenv.
对于您的项目,您应该使用virtualenv。
You can choose which python will be that of the virtualenv at creation time, by specifying it on the command line:
您可以在创建时选择哪个 python 是 virtualenv 的 python,方法是在命令行上指定它:
virtualenv -p python3 env
# then
. env/bin/activate
python # ← will run python3
That python interpreter will be the one used when you run python
or pip
while the virtualenv is active.
当您运行python
或pip
virtualenv 处于活动状态时,将使用该 Python 解释器。
Under the hood, activating the virtualenv will:
在幕后,激活 virtualenv 将:
- modify your
PATH
environment setting so binaries inenv/bin
override those from your system. - modify your
PYTHONHOME
environment setting so python modules are loaded fromenv/lib
.
- 修改您的
PATH
环境设置,以便二进制文件env/bin
覆盖您系统中的二进制文件。 - 修改您的
PYTHONHOME
环境设置,以便从env/lib
.
So python
, pip
and any other package you install with pip
will be run from the virtualenv, with the python version you chose and the package versions you installed in the virtualenv.
因此python
,pip
您安装的任何其他软件包pip
都将从 virtualenv 运行,使用您选择的 python 版本和您在 virtualenv 中安装的软件包版本。
Other than this, running python
without using virtualenv will just run the default python of the system, which you cannot usually change as it would break a lot of system scripts.
除此之外,在python
不使用 virtualenv 的情况下运行只会运行系统的默认 python,您通常无法更改它,因为它会破坏很多系统脚本。
回答by Kevin
Although PEP 394does not specifically mention pip
, it does discuss a number of other Python-related commands (including python
itself). The short version is that, for reasons of backwards compatibility, the unversioned commands should refer to Python 2.x for the immediate future on most reasonable systems.
尽管PEP 394没有具体提到pip
,但它确实讨论了许多其他与 Python 相关的命令(包括python
它自己)。简短的版本是,出于向后兼容性的原因,在大多数合理的系统上,未版本控制的命令应该在不久的将来参考 Python 2.x。
Generally, these aliases are implemented as symbolic links, and you can just flip the symlink to point at the version you want (e.g. with ln -f -s $(which pip3) $(which pip)
as root). But it may not be a good idea if you have any software that expects to interact with Python 2 (which may be more than you think since a lot of software interacts with Python).
通常,这些别名是作为符号链接实现的,您只需翻转符号链接以指向您想要的版本(例如以ln -f -s $(which pip3) $(which pip)
root 身份)。但是,如果您有任何希望与 Python 2 交互的软件(这可能比您想象的更多,因为许多软件与 Python 交互),这可能不是一个好主意。
The saner option is to set up a Virtualenvwith Python 3. Then, within the Virtualenv, all Python-related commands will refer to 3.x instead of 2.x. This will not break the system, unlike the previous paragraph which could well break things.
更明智的选择是使用 Python 3设置Virtualenv。然后,在 Virtualenv 中,所有与 Python 相关的命令都将引用 3.x 而不是 2.x。这不会破坏系统,不像上一段可能会破坏事物。
回答by EzioAuditore
Since you have specified in the comments you want syntax like pip install [package]
to work, here is a solution:
由于您已在注释中指定了您希望使用的语法pip install [package]
,这里有一个解决方案:
Install
setuptools
forPython3
:apt-get install python3-setuptools
Now
pip
forPython3
could be installed by:python3 -m easy_install pip
Now you can use
pip
with the specific version of Python to install package for Python 3 by:pip-3.2 install [package]
安装
setuptools
为Python3
:apt-get install python3-setuptools
现在,
pip
对于Python3
可以通过安装:python3 -m easy_install pip
现在,您可以使用
pip
特定版本的 Python 来安装 Python 3 的软件包:pip-3.2 install [package]