相关文章推荐
坏坏的小狗  ·  nginx 一些配置 - ...·  5 月前    · 
强悍的台灯  ·  amazon web services - ...·  1 年前    · 
酷酷的水煮鱼  ·  C语言 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

brew install python or brew info python output mentions:

Unversioned symlinks python , python-config , pip etc. pointing to python3 , python3-config , pip3 etc., respectively, have been installed into /opt/homebrew/opt/python@3.9/libexec/bin

So running

% echo 'export PATH=/opt/homebrew/opt/python@3.9/libexec/bin:$PATH' >> ~/.zprofile
% source ~/.zprofile
# For bash use ~/.bash_profile.

gets you those symlinks created by Homebrew - python=python3, pip=pip3 etc

% python --version
Python 3.9.10
% pip --version
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
                Heads up to folks that you shouldn't just put this into your path blindly, for example, the directory suggested in this answer does not exist for me. On my system, Python version number is part of the path directory and I had a couple of versions of python installed via homebrew so I needed to choose one to represent python3 in bash profile.
– adamlogan
                Nov 14, 2021 at 23:18
                @adamlogan I have updated answer to include brew info python which gets you current path to add
– rofrol
                Mar 24, 2022 at 12:28
                Note that currently, the brew will install 3.10, so use: echo 'export PATH=/opt/homebrew/opt/python@3.10/libexec/bin:$PATH' >> ~/.zprofile
– Oleksandr Knyga
                Sep 8, 2022 at 21:03
                I thought brew was meant to manage this for us. Like put symlinks in our existing path instead of in some random folder we have to manually add to our path and change if we need to swap versions at all.
– Brad
                Oct 11, 2022 at 6:35
$ brew install python3
==> Downloading http://python.org/ftp/python/3.3.0/Python-3.3.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.3.0.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.3.0 --enable-ipv6 --datarootdir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.3.0/share/python3
==> Downloading https://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz
Already downloaded: /Library/Caches/Homebrew/distribute-0.6.35.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Downloading https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz
Already downloaded: /Library/Caches/Homebrew/pip-1.3.1.tar.gz
==> /usr/local/Cellar/python3/3.3.0/bin/python3.3 -s setup.py install --force --verbose --install-li
==> Caveats
Homebrew's Python3 framework
  /usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework
Distribute and Pip have been installed. To update them
  pip3 install --upgrade distribute
  pip3 install --upgrade pip
To symlink "Idle 3" and the "Python Launcher 3" to ~/Applications
  `brew linkapps`
You can install Python packages with
  `pip3 install <your_favorite_package>`
They will install into the site-package directory
 /usr/local/lib/python3.3/site-packages
Executable python scripts will be put in:
 /usr/local/share/python3
so you may want to put "/usr/local/share/python3" in your PATH, too.
See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python

Once installed update your system PATH variable, add the next line to ~/.bash_profile

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

And then:

$ source ~/.bash_profile

Now launch Python:

$ python3
Python 3.3.0 (default, Mar 26 2013, 10:01:40) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

You can check python3 path:

$ which python3
/usr/local/bin/python3
                If you are installing python 3 using homebrew (using brew install python3), then to run python you should invoke python3 from terminal in other case you will be running python 2.
– jabaldonedo
                Aug 24, 2013 at 14:41
                IMHO using alias here might be confusing as long as python normally refers to version 2, I can't tell if it is a normal behavior, you should ask another question (maybe in superuser.com)
– jabaldonedo
                Aug 24, 2013 at 15:08
                If you dont want to call it python3 and instead keep it simple as python run these  brew unlink python@2 && brew link python@3. then add this to your bash profile export PATH="/usr/local/opt/python/libexec/bin:$PATH"
– Jasmeet Singh
                Mar 8, 2020 at 4:36
                Just tried, if I type 'which python', I got '/usr/bin/python', and 'python --version' got '-bash: python3: command not found'
– photosynthesis
                Aug 24, 2013 at 14:43
                @photosynthesis I just used this method to make 'python' to invoke 'python2.6'. It should work the same way as python3
– Pan Long
                Aug 24, 2013 at 14:49

I came through the same issue and did some research. I found that someone has created a bug for the same issue under the azure/cli repository. You can find that issue here. I am providing the same solution here which was very easy and fixed my issue:

Most probably the Brew is broken and needs some patching or fixing. So run brew doctor command which will give you a summary about what is happening. Below is what I got:

mymac:bin sidmishra$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: The following directories do not exist:
/usr/local/sbin
You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin
Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected dylibs:
  /usr/local/lib/LibSideSyncOSX9.dylib
  /usr/local/lib/ss_conn_lib.dylib
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  python

The awesome stuff about the command brew doctor is that it not only tells you issues but also suggests you the solution steps in most of the cases. So, I ran all the commands suggested by the brew and to link I ran the following command:

brew link python

Above command threw me an error:

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks/Python.framework

It seems that /urs/local/Frameworks doesn't have enough rights for my current user. So, I ran the following command and gave enough rights to my current user:

sudo chown -R $(whoami) /usr/local/Frameworks/

After running above command I ran linking command again, and it worked!!!

mymac$ brew link python
Linking /usr/local/Cellar/python/3.7.1... 1 symlinks created

Now run following command to get the current selected python version:

python --version

Above command should give you 3.7.1 (as of 21st Dec 2018) or new version for the python. There might be a chance that your Mac would have python2 set by default. If the version is not python3 then you have to a couple of steps to use the latest python3 over python2 version. Here are the steps:

Using Shell:

  • Open ~/.bash_loginor ~/.bash_profileor ~/.cshrcor ~/.profileor ~/.tcshrcor ~/.zprofile, whatever shell you are using for commands, in edit mode. You may have to use sudo to edit them.
  • Add following steps to it:

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

  • Also, add following for backup:

    alias python=python3

  • Using homebrew:

    Run following commands to unlink python2 and link python3:

    mymac$ brew unlink python@2
    mymac$ brew link python@3
    

    Above will unlink python2 and link python3.

    Hope some of you will get helped from this answer.

    Good Day!!!

    TL;DR: brew unlink python@2 && brew link python@3 helped me. Other text won't read. Thank you. – kyb May 4, 2020 at 16:27

    This formula installs a python2 executable to /usr/local/bin. If you wish to have this formula's python executable in your PATH then add the following to ~/.bash_profile: export PATH="/usr/local/opt/python/libexec/bin:$PATH"

    Then confirm your python executable corresponds to the correct installation:

    $ which python or

    $ python --version

    Installing with Homebrew is recommended on macOS. That being said, Python 2.7 comes with Mac OS; however, it is deprecated and will be removed soon. As such, you should be using Python3 and newer.

  • Install Homebrew.
  • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    
  • Run brew install python
  • Once Python is installed, Homebrew will say that the installation is complete, but that you already have Python 2.7 installed. This is nice, but we want to set it to actually see python3 as an option

  • Use brew link
  • Confirm by running which python3, the path should be /usr/local/bin/python3
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.