将 python3 作为我在 Mac 上的默认 python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49704364/
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
Make python3 as my default python on Mac
提问by Sambo Kim
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 python3with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
我在这里尝试做的是将python3 作为我的默认 python。除了自动安装在 mac 上的 python 2.7,我用homebrew安装了python3。这是我关注的网站。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. But here's my little confusion occurs.
我想我很好地遵循了每条指令,重新安装了 xcode、命令行工具和自制软件。但这里发生了我的小困惑。
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. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
该脚本将解释它将进行哪些更改并在安装开始之前提示您。安装 Homebrew 后,在 PATH 环境变量的顶部插入 Homebrew 目录。您可以通过在 ~/.profile 文件底部添加以下行来完成此操作
导出路径=/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. So I opened the ~/.profilefile by open .profilein the terminal, and added following line at the bottom. And now it looks like this.
我真的很困惑这是什么,但我得出结论,我应该在 ~/.profile 文件的底部添加以下行。所以我通过在终端中open .profile打开了~/.profile文件,并在底部添加了以下行。现在它看起来像这样。
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 python3when I do python --version.But it just shows me python 2.7.10.I want my default python to be python3not 2.7
然后我做了brew install python,并希望在我做python --version时看到python3 。但它只是向我展示了python 2.7.10。我希望我的默认 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?Could someone explain in simple English what PATHexactly is and how I could make my default python to be python3 when I run python --versionin the terminal?
也许它与PATH 有关系?有人可以用简单的英语解释一下PATH到底是什么,以及当我在终端中运行python --version时如何使我的默认 python 成为 python3吗?
回答by nbari
Probably the safest and easy way is to use brewand 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
回答by Jonathan
According to this S.O. 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 linkmight 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”。
回答by Krunal Rajkotiya
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
python --version
蟒蛇 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
通过替换 Ubuntu 中的 python2 使 python3 成为默认 python。
- Open Terminal
cd
nano ~/.bashrc
alias python=python3
(Add this line on top of .bashrc file)- Press
ctr+o
(To save the file) - Press
Enter
- Press
ctr+x
(To exit the file) source ~/.bashrc
OR. ~/.bashrc
(To refresh the bashrc file)
- 打开终端
cd
nano ~/.bashrc
alias python=python3
(在 .bashrc 文件顶部添加这一行)- 按
ctr+o
(保存文件) - 按
Enter
- 按
ctr+x
(退出文件) source ~/.bashrc
或. ~/.bashrc
(刷新 bashrc 文件)
python --version
Python 3.7.5
python --version
蟒蛇 3.7.5
回答by Reinstate Monica
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create a command line alias
.
在系统范围内更改默认的 python 版本可能会破坏一些依赖于 python2 的应用程序。另一种解决方案是创建一个命令行alias
。
open terminal and make sure you are in the root user folder (eg.
MacBook-Pro:~ jane$
)run
open .bash_profile
. If the file doesn't exist, runtouch .bash_profile
first.now in
.bash_profile
typealias <yourAliasName>="python3"
打开终端并确保您位于根用户文件夹中(例如
MacBook-Pro:~ jane$
)跑
open .bash_profile
。如果文件不存在,touch .bash_profile
请先运行。现在在
.bash_profile
类型alias <yourAliasName>="python3"
You can name <yourAliasName>
whatever you want, not just python
.
您可以随意命名<yourAliasName>
,而不仅仅是python
.