[英]How to override the pip command to Python3.x instead of Python2.7?
I am using OSX and I have pip installed for both Python3.5 and Python2.7.我正在使用 OSX,并且为 Python3.5 和 Python2.7 安装了 pip。 I know I can run the command pip2
to use Python2 and when I use the command pip3
Python3.x will be used.我知道我可以运行命令pip2
来使用 Python2,当我使用命令pip3
时,将使用 Python3.x。 The problem is that the default of pip
is set to Python2.7 and I want it to be 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.如果它是一个虚拟环境,我可以只运行 Python3.x 而忘记所有关于 Python2.7 的事情,不幸的是,因为 OSX 需要 Python2.7 才能使用我不能这样做。 Hence why I'm asking this.这就是为什么我要问这个。
Thanks for the answer.谢谢你的回答。 I however don't want to change what running python
does.但是,我不想更改运行python
功能。 Instead I would like to change the path that running pip
takes.相反,我想更改运行pip
的路径。 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.目前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)
,但我正在寻找pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (python 3.5)
我相信一定有办法做到这一点。 Any ideas?有什么想法吗?
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
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. -m
调用指定包的__main__.py
模块。 Pip supports this.皮普支持这一点。
Can't you alias pip='pip3'
in your ~/.bash_profile
?你不能在你的~/.bash_profile
alias pip='pip3'
吗?
In Terminal, run nano ~/.bash_profile
, then add a line to the end that reads alias pip='pip3'
.在终端中,运行nano ~/.bash_profile
,然后在末尾添加一行,读取alias pip='pip3'
。 This is safe;这是安全的; it won't affect system processes, only your terminal.它不会影响系统进程,只会影响您的终端。
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.当您在 virtualenv 处于活动状态时运行python
或pip
时,将使用该 python 解释器。
Under the hood, activating the virtualenv will:在幕后,激活 virtualenv 将:
PATH
environment setting so binaries in env/bin
override those from your system.修改您的PATH
环境设置,以便env/bin
二进制文件覆盖您系统中的二进制文件。PYTHONHOME
environment setting so python modules are loaded from env/lib
.修改您的PYTHONHOME
环境设置,以便从env/lib
加载 python 模块。 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.除此之外,在不使用 virtualenv 的情况下运行python
只会运行系统的默认 python,你通常不能更改它,因为它会破坏很多系统脚本。
Although PEP 394 does not specifically mention pip
, it does discuss a number of other Python-related commands (including python
itself).尽管PEP 394没有特别提到pip
,但它确实讨论了许多其他与 Python 相关的命令(包括python
本身)。 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.简短的版本是,出于向后兼容性的原因,在大多数合理的系统上,未版本控制的命令应该在不久的将来参考 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 (eg with ln -f -s $(which pip3) $(which pip)
as root).通常,这些别名是作为符号链接实现的,您只需翻转符号链接以指向您想要的版本(例如,使用ln -f -s $(which pip3) $(which pip)
作为根)。 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).但是,如果您有任何希望与 Python 2 交互的软件(这可能比您想象的更多,因为许多软件与 Python 交互),这可能不是一个好主意。
The saner option is to set up a Virtualenv with Python 3. Then, within the Virtualenv , all Python-related commands will refer to 3.x instead of 2.x.更明智的选择是使用 Python 3 设置Virtualenv 。然后,在 Virtualenv 中,所有与 Python 相关的命令都将引用 3.x 而不是 2.x。 This will not break the system, unlike the previous paragraph which could well break things.这不会破坏系统,不像上一段可能会破坏事物。
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
for Python3
: apt-get install python3-setuptools
为Python3
安装setuptools
: apt-get install python3-setuptools
Now pip
for Python3
could be installed by: python3 -m easy_install pip
现在可以通过以下方式安装Python3
pip
: 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]
现在你可以使用pip
和特定版本的 Python 来安装 Python 3 的包: pip-3.2 install [package]
It works for me:它对我有用:
As super-user作为超级用户
Uninstall pip卸载pip
sudo pip uninstall pip
Install pip安装点
sudo python3 -m pip install --upgrade --force pip
Check install path检查安装路径
sudo pip -V
As local-user作为本地用户
Uninstall pip卸载pip
pip uninstall pip
Install pip安装点
python3 -m pip install --upgrade --force pip
Check install path检查安装路径
pip -V
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.