简体   繁体   English

在 Mac 上将 python3 设为我的默认 python

[英]Make python3 as my default python on Mac

What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew .我在这里要做的是将python3 作为我的默认值 python。除了自动安装在 mac 上的 python 2.7,我用homebrew安装了python3 This is the website that I'm following.这是我关注的网站。 http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx

I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew.我想我很好地遵循了每条说明,全新安装了 xcode、命令行工具和自制软件。 But here's my little confusion occurs.但这是我的一点困惑。

The script will explain what changes it will make and prompt you before the installation begins.该脚本将解释它将进行哪些更改,并在安装开始前提示您。 Once you've installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable.安装 Homebrew 后,将 Homebrew 目录插入 PATH 环境变量的顶部。 You can do this by adding the following line at the bottom of your ~/.profile file您可以通过在 ~/.profile 文件底部添加以下行来执行此操作

export PATH=/usr/local/bin:/usr/local/sbin:$PATH导出 PATH=/usr/local/bin:/usr/local/sbin:$PATH

I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file.我真的很困惑这是什么,但我的结论是我应该在 ~/.profile 文件的底部添加下面这行。 So I opened the ~/.profile file by open.profile in the terminal, and added following line at the bottom.所以我在终端中通过open.profile打开了~/.profile文件,并在底部添加了以下行。 And now it looks like this.现在它看起来像这样。

export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

And then I did brew install python , and was hoping to see python3 when I do python --version.然后我执行了brew install python ,并希望在执行python --version时看到python3 But it just shows me python 2.7.10.但它只显示python 2.7.10。 I want my default python to be python3 not 2.7我希望我的默认 python 是python3而不是 2.7

And I found a little clue from the website.我从网站上找到了一点线索。

Do I have a Python 3 installed?我安装了 Python 3 吗?

$ python --version
Python 3.6.4

If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/如果您仍然看到 2.7,请确保在PATH /usr/local/bin/ 中优先于 /usr/bin/

Maybe it has to do something with PATH?也许它必须与PATH 做些什么? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?有人可以用简单的英语解释PATH到底是什么,以及当我在终端中运行python --version时如何使我的默认 python 成为 python3?

Probably the safest and easy way is to use brew and then just modify your PATH :可能最安全和简单的方法是使用brew然后修改你的PATH

First update brew:第一次更新brew:

brew update

Next install python:接下来安装python:

brew install python

That will install and symlink python3 to python, for more details do:这将安装python3并将其符号链接到python,有关更多详细信息,请执行以下操作:

brew info python

Look for the Caveats:寻找注意事项:

==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

Then add to your path /usr/local/opt/python/libexec/bin :然后添加到您的路径/usr/local/opt/python/libexec/bin

export PATH=/usr/local/opt/python/libexec/bin:$PATH

The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python PATH的顺序很重要,首先/usr/local/opt/python/libexec/bin将有助于优先安装 brew install (python3),而不是位于/usr/bin/python

Before we make the changes, the default version of python in my system was python 2.7.17.在我们进行更改之前,我系统中的默认 python 版本是 python 2.7.17。

python --version

Python 2.7.17蟒蛇 2.7.17

To make python3 as default python by replacing python2 in Ubuntu.通过替换 Ubuntu 中的 python2 使 python3 成为默认 python。

  1. Open Terminal打开终端
  2. cd
  3. nano ~/.bashrc
  4. alias python=python3 (Add this line on top of .bashrc file) alias python=python3 (在 .bashrc 文件的顶部添加这一行)
  5. Press ctr+o (To save the file)ctr+o (保存文件)
  6. Press EnterEnter
  7. Press ctr+x (To exit the file)ctr+x (退出文件)
  8. source ~/.bashrc OR . ~/.bashrc source ~/.bashrc. ~/.bashrc . ~/.bashrc (To refresh the bashrc file) . ~/.bashrc (刷新bashrc文件)

python --version

Python 3.7.5蟒蛇 3.7.5

Changing the default python version system wide can break some applications that depend on python2 .在系统范围内更改默认的 python 版本可能会破坏一些依赖于python2应用程序。 The alternative solution would be to create an alias .另一种解决方案是创建alias

If you are using zsh (the default on Mac OS ) run the following from terminal:如果您使用的是zshMac OS上的默认设置),请从终端运行以下命令:

echo 'alias python="python3"' >> ~/.zshrc

According to this SO post , changing the default Python interpreter could possibly break some applications that depend on Python 2.根据这篇 SO post ,更改默认 Python 解释器可能会破坏某些依赖 Python 2 的应用程序。

The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.该帖子还提到使用别名作为解决方案,此链接也可能是有关如何执行此操作的很好参考。

Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".就个人而言,我只是在运行脚本或进入 shell 环境之前输入“Python3”而不是“python”。

For sure this answer is not very different from others, but I would like to recommend that当然,这个答案与其他答案没有太大不同,但我想建议

alias python='python3'

It simply works for me.它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM