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

前端 未结 21 1698
滥情空心
滥情空心 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:22

    I'm a little late to the game on this one, but I thought I should post an updated answer since I just encountered this issue for myself. Please note that this will only apply to a Mac-based setup (I haven't tried it with Windows or any flavor of Linux).

    The simplest way to get this working is to install Python via Brew. If you don't have brew installed, you will need to do that first. Once installed, do the following in at the terminal:

    brew install python
    

    This will install Python 3. After it's installed, run this:

    ls -l /usr/local/bin/python*
    

    You will see all of the links created by brew to its Python install. It will look something like this:

    lrwxr-xr-x  1 username  admin  36 Oct  1 13:35 /usr/local/bin/python3@ -> ../Cellar/python/3.7.4_1/bin/python3
    lrwxr-xr-x  1 username  admin  43 Oct  1 13:35 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.4_1/bin/python3-config
    lrwxr-xr-x  1 username  admin  38 Oct  1 13:35 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.4_1/bin/python3.7
    lrwxr-xr-x  1 username  admin  45 Oct  1 13:35 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7-config
    lrwxr-xr-x  1 username  admin  39 Oct  1 13:35 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.4_1/bin/python3.7m
    lrwxr-xr-x  1 username  admin  46 Oct  1 13:35 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7m-config
    

    The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:

    ln -s -f /usr/local/bin/python3 /usr/local/bin/python
    

    Once set, you can do:

    which python
    

    and it should show:

    /usr/local/bin/python
    

    You will have to reload your current terminal shell for it to use the new symlink in that shell, however, all newly opened shell sessions will (should) automatically use it. To test this, open a new terminal shell and run the following:

    python --version
    
    0 讨论(0)
  • 2020-11-22 08:23
    $ sudo ln -s -f $(which python3) $(which python)
    

    done.

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

    On MacOS

    Step-1: Upgrade python to latest version by: $ brew upgrade python

    Step-2: Go to home: $ cd

    Step-3: open .bash_profile

    $ vi .bash_profile

    Setting PATH for Python 3.8

    PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}" export PATH

    Step-4: Save the file. And compile it by:

    $ . .bash_profile

    Step-5: Check the python version:

    $ python -V

    Step-6: Thats all.

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

    Open ~/.bash_profile file.

    vi ~/.bash_profile
    

    Then put the alias as follows:

    alias python='python3'
    

    Now save the file and then run the ~/.bash_profile file.

    source ~/.bash_profile
    

    Congratulation !!! Now, you can use python3 by typing python.

    python --version
    

    Python 3.7.3

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

    Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:

    pyenv global 3.8.2
    

    and then in any specific project, you can create a .python-version file which has the python version inside of it:

    pyenv local 2.7.1
    

    This is the best way to manage multiple versions of python on a system in my opinion.

    0 讨论(0)
  • Well... It's kinda old. But still deserves a good answer.

    And the good one is You Don't Wanna Touch The Default Python On Mac.

    Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it's still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide... whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.

    The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency... hope you understand what I mean. A good developer doesn't care. Good developers create clean playgrounds with python version they desire.

    0 讨论(0)
提交回复
热议问题