在 Mac 上将 python3作为默认的 python

除了自动安装在 Mac 上的 python 2.7之外,我还用 自酿的安装了 蟒蛇3。这是我关注的网站。http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx

我想我很好地遵循了每条指令,新安装了 xcode、命令行工具和自制程序。但我有点困惑。

该脚本将解释它将进行哪些更改,并在安装开始之前提示您。一旦你安装了“家酿”,在 PATH 环境变量的顶部插入“家酿”目录。可以通过在 ~/的底部添加以下代码行来实现这一点。档案

Export PATH =/usr/local/bin:/usr/local/sbin: $PATH

我真的搞不清楚这是什么,但是我得出结论,我应该在 ~/的底部添加以下一行。档案。因此,我在终端中通过 打开,侧面打开了 ~/. 配置文件文件,并在底部添加了以下行。现在看起来像这样。

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

然后我做了 酿造安装 Python,并希望看到 蟒蛇3当我做 < strong > python-version。 但它只显示了 Python 2.7.10.,我希望我的默认 Python 是 蟒蛇3,而不是2.7

我在网站上找到了一点线索。

我是否安装了 Python 3?

$ python --version
Python 3.6.4

如果你仍然看到2.7确保在 PATH/usr/local/bin/优先于/usr/bin/

也许它必须与 路径?做一些事情,谁能用简单的英语解释一下 路径到底是什么,以及当我在终端运行 巨蟒,版本时,我如何使我的默认 python 变成 python3?

125815 次浏览

According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on 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".

Probably the safest and easy way is to use brew and then just modify your PATH:

First update brew:

brew update

Next install python:

brew install python

That will install and symlink python3 to python, for more details do:

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:

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

Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.

If you are using zsh (the default on Mac OS) run the following from terminal:

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

Before we make the changes, the default version of python in my system was python 2.7.17.

python --version

Python 2.7.17

To make python3 as default python by replacing python2 in Ubuntu.

  1. Open Terminal
  2. cd
  3. nano ~/.bashrc
  4. alias python=python3 (Add this line on top of .bashrc file)
  5. Press ctr+o (To save the file)
  6. Press Enter
  7. Press ctr+x (To exit the file)
  8. source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)

python --version

Python 3.7.5