Skip to content

Instantly share code, notes, and snippets.

@fscm
Last active January 27, 2022 13:15
Show Gist options
  • Save fscm/37fe2ade6a4e5488fb7541133cbd92bf to your computer and use it in GitHub Desktop.
Save fscm/37fe2ade6a4e5488fb7541133cbd92bf to your computer and use it in GitHub Desktop.
[macOS] Install Python 3 programming language

[macOS] Install Python 3 programming language

Instructions on how to install the Python 3 programming language on macOS.

Uninstall

First step should be to unsinstall any previous Python 3 installation. This step can be skipped if no Python 3 version was previously installed.

This will completelly remove any Python 3 previously installed. If you which to keep the user modules installed with pip3 you should skip the last command.

To uninstall any previous Python 3 installations use the following commands:

sudo find /usr/local/bin -lname '*/Library/Frameworks/Python.framework/Versions/3*' -delete
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /Applications/Python*
sudo rm -rf ${HOME}/Library/Python

Install

The Python programming language can be obtained here. Copy the link for the package version that you want to install from there.

Get the Python installer package using the following commands:

mkdir ~/Downloads/Python
(cd ~/Downloads/Python && curl --silent --location --retry 3 --remote-name "https://www.python.org/ftp/python/3.10.1/python-3.10.1-macos11.pkg")

Extract the install choices using the following command:

installer -pkg ~/Downloads/Python/python-*.pkg -target / -showChoiceChangesXML > ~/Downloads/Python/choicesfile.xml

Change the install choices to install only the Python Framework and the Pip tool (no documentation or IDE will be installed) using the following commands:

sed \
  -i '.orig' \
  -e 's,<integer>1</integer>,<integer>0</integer>,g' \
  -E \
  -n \
  -e '/<dict>/,/<\/dict>/!{p;}; /<dict>/,/<\/dict>/{H;}; /<\/dict>/{s/.*//;x;};/selected.*Python(Framework|UnixTools|InstallPip)/{s/>0</>1</;}; /<\/dict>/{p;};' \
  ~/Downloads/Python/choicesfile.xml

Run the installer package using the following command:

sudo installer -pkg ~/Downloads/Python/python-*.pkg -target / -applyChoiceChangesXML ~/Downloads/Python/choicesfile.xml

Configure

Add the following lines to the .bash_profile file:

# make python use UTF-8 encoding
export PYTHONIOENCODING='UTF-8'
# add python tools to path
export PATH="${PATH}:$(python3 -m site --user-base)/bin"

Run the following command to enable SSL connections:

sudo ln -s /private/etc/ssl/cert.pem /Library/Frameworks/Python.framework/Versions/Current/etc/openssl/cert.pem

Verify

Open a new terminal window and check if the Python 3 programming language is installed:

python3 --version
pip3 --version

Clean up

After installing the Python 3 programing language you can remove the downloaded installation package using the following command:

rm -rf ~/Downloads/Python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment