How to install Python via Homebrew on Mac

Once Homebrew is installed, run the command brew update to ensure that you have the latest package information and formulas.

brew update

The output will be like this:

Updated 2 taps (homebrew/core and homebrew/cask).
…….

To install Python, run the command brew install python — this will install the latest version of Python available in Homebrew’s database.

brew install python

If you want to install a specific version of Python, run the command brew install python@version. Let’s say you want to install Python’s 3.10 version, run the brew install python@3.10 command:

brew install python@3.10

The output of the above command will be like this:

==> Fetching python@3.10
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.10/manifests/3.10.9
Already downloaded: /Users/.../Library/Caches/Homebrew/downloads/fea5100db04d223d92afe7166db6ed7d5cfd033e941b490f34f12a9ba6ba99c9--python@3.10-3.10.9.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.10/blobs/sha256:b6bfa7
Already downloaded: /Users/..../Library/Caches/Homebrew/downloads/7a53e3a2faeb75713663f1cd1fe5ec67dc045ec62ad1b87db042c55dc54b13b7--python@3.10--3.10.9.arm64_ventura.bottle.tar.gz
==> Pouring python@3.10--3.10.9.arm64_ventura.bottle.tar.gz
==> /opt/homebrew/Cellar/python@3.10/3.10.9/bin/python3.10 -m ensurepip
==> /opt/homebrew/Cellar/python@3.10/3.10.9/bin/python3.10 -m pip install -v --n
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

i.e Python has been installed at the /opt/homebrew/bin/python3 location.

Creates a symbolic link (shortcut) to the Python 3 executable in the /usr/local/bin directory, using the name “python” with ln -s /usr/local/bin/python3 /usr/local/bin/python command:

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

If you want to use Python 3 as your default Python version on your system, but some of your scripts or programs are written with the assumption that the “python” command will run Python 2, you can use the above command to make them work with both versions.

Python has been installed via Homebrew on your MAC now. After the installation is completed with the above steps, you can check the version of python by running the command python -V

python -V

The output will be like this: Python 3.10.9

To test the Python, open a new Terminal window and type python.

python

The output will show you the version of python along with the Python terminal to play with:

Python 3.10.9 (v3.10.9:1dd9be6584, Dec  6 2022, 14:37:36) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

You can start typing print(“Hello World”) for example to see if it is working and the terminal will give you the Hello World output from that command:

>>> print(“Hello World”)

Output: Hello World

If you install Python using Homebrew, the version of Python that comes with OS X will be replaced with Homebrew’s version of Python. This can cause problems if you rely on a different version of Python for other projects.