如何在OS X上将Python的默认版本设置为3.x?

本文翻译自:How to set Python's default version to 3.x on OS X?

I'm running Mountain Lion and the basic default Python version is 2.7. 我正在运行Mountain Lion,而基本的默认Python版本是2.7。 I downloaded Python 3.3 and want to set it as default. 我下载了Python 3.3,并希望将其设置为默认值。

Currently: 目前:

$ python
    version 2.7.5
$ python3.3
    version 3.3

How do I set it so that every time I run $ python it opens 3.3? 如何设置它,以便每次运行$ python都打开3.3?


#1楼

参考:https://stackoom.com/question/1fjhx/如何在OS-X上将Python的默认版本设置为-x


#2楼

Go to 'Applications', enter 'Python' folder, there should be a bash script called 'Update Shell Profile.command' or similar. 转到“应用程序”,进入“ Python”文件夹,应该有一个名为“ Update Shell Profile.command”或类似名称的bash脚本。 Run that script and it should do it. 运行该脚本,它应该这样做。

Update: It looks like you should not update it: how to change default python version? 更新:看来您不应该更新它: 如何更改默认python版本?


#3楼

Changing the default python version system-wide would break some applications that depend on python2. 在系统范围内更改默认的python版本会破坏某些依赖python2的应用程序。

You can alias the commands in most shells, Mac OS X uses bash by default. 您可以在大多数shell中为命令添加别名,Mac OS X默认使用bash。 If you also do, put this into your ~/.bash_profile : 如果您也这样做,请将其放入~/.bash_profile

alias python='python3'

python command now refers to python3 . python命令现在引用python3 If you want the original python (that refers to python2 ), you can escape the alias ie doing \\python will launch python2 leaving the alias untouched) 如果您想要原始的python(指的是python2 ),则可以转义别名,即执行\\python将启动python2 ,而别名保持不变。

If you launch interpreters more often (I do) , better is to: 如果您更频繁地启动口译员(我愿意) ,最好是:

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

Open a new shell session and these aliases will be available. 打开一个新的Shell会话,这些别名将可用。 If you want it to be effective in current session then you'll have to source it: 如果您希望它在当前会话中有效,则必须提供它:

source ~/.bash_profile

Tip: Instead of doing: 提示:不要这样做:

#!/usr/bin/env python

use: 使用:

#!/usr/bin/env python3

the system will use python3 for running python executables . 系统将使用python3运行python 可执行文件


#4楼

I'm not sure if this is available on OS X, but on linux I would make use of the module command. 我不确定在OS X上是否可用,但是在linux上,我将使用module命令。 See here . 看这里

Set up the modulefile correctly, then add something like this to your rc file (eg ~/.bashrc): 正确设置modulefile,然后将类似这样的内容添加到rc文件中(例如〜/ .bashrc):

module load python3.3

This will make it so that your paths get switched around as required when you log in without impacting any system defaults. 这样一来,登录时即可根据需要切换路径,而不会影响任何系统默认值。


#5楼

I think when you install python it puts export path statements into your ~/.bash_profile file. 我认为,当您安装python时,会将导出路径语句放入〜/ .bash_profile文件中。 So if you do not intend to use Python 2 anymore you can just remove that statement from there. 因此,如果您不再打算使用Python 2,则可以从那里删除该语句。 Alias as stated above is also a great way to do it. 如上所述的别名也是一种很好的方法。

Here is how to remove the reference from ~/.bash_profile - vim ./.bash_profile - remove the reference (AKA something like: export PATH="/Users/bla/anaconda:$PATH") - save and exit - source ./.bash_profile to save the changes 这是从〜/ .bash_profile中删除引用的方法-vim ./.bash_profile-删除引用(又名类似:export PATH =“ / Users / bla / anaconda:$ PATH”)-保存并退出-源./ .bash_profile保存更改


#6楼

You can solve it by symbolic link. 您可以通过符号链接解决它。

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值