我正在运行 Mountain Lion,基本的默认 Python 版本是 2.7。我下载了 Python 3.3 并希望将其设置为默认值。
目前:
$ python
version 2.7.5
$ python3.3
version 3.3
如何设置,以便每次运行 $ python
时打开 3.3?
最佳答案
在系统范围内更改默认 python 可执行文件的版本可能会破坏一些依赖于 python2 的应用程序。
但是,您可以在大多数 shell 中为命令设置别名,因为 macOS 中的默认 shell(10.14 及更低版本中的 bash;10.15 中的 zsh)共享类似的语法。你可以把
别名 python='python3'
在你的 ~/.profile
中,然后在你的 ~/.bash_profile
和/或你的 ~/中源
有这样一行:~/.profile
.zsh_profile
[ -e ~/.profile ] && . ~/.profile
这样,您的别名将跨 shell 工作。
有了这个,python
命令现在调用 python3
。如果你想偶尔调用“原始”python(指 python2),你可以使用 command python
,它不会改变别名,并且适用于所有贝壳。
如果你更频繁地启动解释器(我愿意),你总是可以创建更多的别名来添加,即:
alias 2='python2'
alias 3='python3'
提示:对于脚本,不要使用 shebang,例如:
#!/usr/bin/env python
使用:
#!/usr/bin/env python3
这样,系统将使用python3来运行python可执行文件。
关于python - 如何在 OS X 上将 Python 的默认版本设置为 3.x?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18425379/