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

查看:100
本文介绍了如何在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.

但是,您可以在大多数外壳程序中为命令加上别名,因为macOS中的默认外壳程序(10.14及以下版本中的bash; 10.15中的zsh)具有相似的语法.你可以放 别名python ='python3' ~/.profile中的内容,然后在~/.bash_profile和/或~/.zsh_profile中的源~/.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 ),则可以使用command 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天全站免登陆