Saturday, January 16, 2021

How to install Python3 on macOS


You do not need to install or configure anything else to use Python 2. As you can see below, there is already Python 2.7.x version on macOS X.

AnalysisMan:/Users/analysisman% python --version
Python 2.7.16


Now let's install Python 3.x version.

Step 1. Install Homebrew


Homebrew is an essential package for Mac users. Homebrew allows us to install the latest and updated version of popular applications, packages, and developer tools.

Here is a link to how to install Homebrew on macOS.


Step 2. Install Python3 with a brew command


brew install python3
This will take a minute or two.

AnalysisMan:/Users/analysisman% brew install python3
Error:
homebrew-core is a shallow clone.
homebrew-cask is a shallow clone.
To `brew update`, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for the inconvenience!
Error: [email protected]: no bottle available!
You can try to install from source with e.g.
brew install --build-from-source [email protected]
Please note building from source is unsupported. You will encounter build
failures with some formulae. If you experience any issues please create pull
requests instead of asking for help on Homebrew's GitHub, Twitter or any other
official channels.



If you see the above error, try out the following command.

AnalysisMan:/Users/analysisman% brew install --build-from-source [email protected]

Error:
homebrew-core is a shallow clone.
homebrew-cask is a shallow clone.
To `brew update`, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2_3.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/80f141e695f73bd058fd82e9f539dc67471666ff6800c5e280b5af7d3050f435?response-
######################################################################## 100.0%


… snipped…

> Downloading https://files.pythonhosted.org/packages/cb/5f/ae1eb8bda1cde4952bd12e468ab8a254c345a0189402bf1421457577f4f3/pip-20.3.1
######################################################################## 100.0%
==> Downloading https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
######################################################################## 100.0%
Error: Xcode alone is not sufficient on Catalina.
Install the Command Line Tools:
xcode-select --install



If you get an error as below, install the Xcode by 'xcode-select --install'.
Error: Xcode alone is not sufficient on Catalina.

Step 3. Make an alias for Python3 or link by symbolic link


Now you have two Python versions. The default Python 2.7.x and the newly installed Python 3.x.
If you want to make the Python 3.x as default. Here are two options.

Option 1) Alias in your Shell


You can alias the commands in most Shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15). You could put alias python='python3' in your ~/.bash_profile, and then source ~/.bash_profile in your ~/.bash_profile or ./.zshrc and source ~/.zshrc.

# bash
vi ~/.bash_profile

# zsh
vi ~/.zshrc

alias python='python3'


Option 2) Link the python with a symbolic link


unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.9 /usr/local/bin/python


After you did Option 1 or 2, you should be able to see your Python version has changed with the installed version.

AnalysisMan:/Users/analysisman% python --version
Python 3.9.2


No comments: