Make python3 as my default python on Mac

后端 未结 4 1486
谎友^
谎友^ 2020-11-30 18:23

What I\'m trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3

相关标签:
4条回答
  • 2020-11-30 18:53

    Probably the safest and easy way is to use brew and then just modify your PATH:

    First update brew:

    brew update
    

    Next install python:

    brew install python
    

    That will install and symlink python3 to python, for more details do:

    brew info python
    

    Look for the Caveats:

    ==> Caveats
    Python has been installed as
      /usr/local/bin/python3
    
    Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
    `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
      /usr/local/opt/python/libexec/bin
    

    Then add to your path /usr/local/opt/python/libexec/bin:

    export PATH=/usr/local/opt/python/libexec/bin:$PATH
    

    The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python

    0 讨论(0)
  • 2020-11-30 18:54

    Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create a command line alias.

    • open terminal and make sure you are in the root user folder (eg. MacBook-Pro:~ jane$ )

    • run open .bash_profile. If the file doesn't exist, run touch .bash_profile first.

    • now in .bash_profile type alias <yourAliasName>="python3"

    You can name <yourAliasName> whatever you want, not just python.

    0 讨论(0)
  • 2020-11-30 18:58

    According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.

    The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.

    Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

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

    Before we make the changes, the default version of python in my system was python 2.7.17.

    python --version

    Python 2.7.17

    To make python3 as default python by replacing python2 in Ubuntu.

    1. Open Terminal
    2. cd
    3. nano ~/.bashrc
    4. alias python=python3 (Add this line on top of .bashrc file)
    5. Press ctr+o (To save the file)
    6. Press Enter
    7. Press ctr+x (To exit the file)
    8. source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)

    python --version

    Python 3.7.5

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