brew-installed Python not overriding system python

后端 未结 3 1827
生来不讨喜
生来不讨喜 2021-02-01 23:38

I just used brew to install Python 3 on OS X. The python3 command now starts the interpreter using brew Python 3.6, but python still opens the interpr

相关标签:
3条回答
  • 2021-02-02 00:00

    TL;DR Add the following to your .bash_profile (or equivalent):

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

    Explanation

    It seems python via homebrew is now handled differently (see https://docs.brew.sh/Homebrew-and-Python).

    • python3 points to Homebrew’s Python 3.x (if installed)
    • python2 points to Homebrew’s Python 2.7.x (if installed)
    • python points to Homebrew’s Python 2.7.x (if installed) otherwise the macOS system Python. Check out brew info python if you wish to add Homebrew’s 3.x python to your PATH.

    Checking out brew info python hints at what you need to do:

    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

    The hint being that you therefore have to add /usr/local/opt/python/libexec/bin before /usr/bin in your path (not /usr/local/bin as stated in some sources e.g. https://docs.python-guide.org/starting/install3/osx/)

    See also https://github.com/Homebrew/homebrew-core/issues/15746

    0 讨论(0)
  • 2021-02-02 00:17

    I tried a few of the proposed solutions in How to link home brew python version and set it as default, but none of them worked. Ultimately I solved this by symlinking python3 --> python:

    ln -s /usr/local/bin/python3 /usr/local/bin/python
    
    0 讨论(0)
  • 2021-02-02 00:20

    One-liner to get homebrew python working:

    zsh

    echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc
    

    bash

    echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
    

    Explanation:
    >> filename appends at the end of the file
    source filename reloads the file

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