如何在 OS X 上将 Python 的默认版本设置为 3.x? [英] How to set Python's default version to 3.x on OS X?

查看:27
本文介绍了如何在 OS X 上将 Python 的默认版本设置为 3.x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Mountain Lion,基本的默认 Python 版本是 2.7.我下载了 Python 3.3 并想将其设置为默认值.

I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.

目前:

$ python
    version 2.7.5
$ python3.3
    version 3.3

如何设置以便每次运行 $ python 时都打开 3.3?

How do I set it so that every time I run $ python it opens 3.3?

推荐答案

在系统范围内更改默认 python 可执行文件的版本可能会破坏一些依赖于 python2 的应用程序.

Changing the default python executable's version system-wide could break some applications that depend on python2.

但是,您可以在大多数 shell 中为命令设置别名,因为 macOS 中的默认 shell(10.14 及更低版本中的 bash;10.15 中的 zsh)共享类似的语法.你可以把别名 python='python3'在你的 ~/.profile 中,然后在你的 ~/.bash_profile 和/或你的 ~/中 source ~/.profile.zsh_profile 带有如下一行:

However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put alias python='python3' in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:

[ -e ~/.profile ] && . ~/.profile

这样,您的别名将可以跨 shell 工作.

This way, your alias will work across shells.

有了这个,python 命令现在调用 python3.如果您想调用原始"python(指的是python2)有时,您可以使用命令python,这将保持别名不变,并且适用于所有shell.

With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.

如果你更频繁地启动解释器(我愿意),你总是可以创建更多的别名来添加,即:

If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:

alias 2='python2'
alias 3='python3'


提示:对于脚本,不要使用像这样的shebang:


Tip: For scripts, instead of using a shebang like:

#!/usr/bin/env python

使用:

#!/usr/bin/env python3

这样,系统将使用python3来运行python可执行文件.

This way, the system will use python3 for running python executables.

这篇关于如何在 OS X 上将 Python 的默认版本设置为 3.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆