How to set Python's default version to 3.x on OS X?

前端 未结 21 1682
滥情空心
滥情空心 2020-11-22 07:52

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.

Currently:

$ python
           


        
21条回答
  •  无人及你
    2020-11-22 08:14

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

    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
    

    This way, your alias will work across shells.

    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'
    

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

    #!/usr/bin/env python
    

    use:

    #!/usr/bin/env python3
    

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

提交回复
热议问题