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

前端 未结 21 1696
滥情空心
滥情空心 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.

    0 讨论(0)
  • 2020-11-22 08:14

    If you are using a virtualenvwrapper, you can just locate it using which virtualenvwrapper.sh, then open it using vim or any other editor then change the following

    # Locate the global Python where virtualenvwrapper is installed.
    if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
    then
        VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
    fi
    

    Change the line VIRTUALENVWRAPPER_PYTHON="$(command \which python)" to VIRTUALENVWRAPPER_PYTHON="$(command \which python3)".

    0 讨论(0)
  • 2020-11-22 08:16

    For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.

    install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3

    0 讨论(0)
  • 2020-11-22 08:17

    I'm not sure if this is available on OS X, but on linux I would make use of the module command. See here.

    Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.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.

    0 讨论(0)
  • 2020-11-22 08:18

    Go to terminal type:

    alias python=python3.x
    

    This will setup default python as python3.x

    0 讨论(0)
  • 2020-11-22 08:19

    If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:

    How to: Macports select python

    TL;DR:

    sudo port select --set python python27
    
    0 讨论(0)
提交回复
热议问题