Karl SvenssonBlogLinkedIn

Installing Python 3.x on macOS

Written 2018-12-08 by Kalle
2 min read | 389 words

This post describes how to install Python 3.x on a freshly installed Mac computer.

Documentation

The source that guided me through the process was the documentation on the official Python website.

Installation

Download and install the latest version of Python 3.x from here.

The installation creates the directory /Applications/Python3.x/ which includes, among other things, the IDLE IDE, the Python Launcher application used for launching scrips via drag-and-drop or double clicking, and a script named Update Shell Profile.command.

Setting up the PATH

The pre-installed version of Python that is bundled with macOS (2.7.10) is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python.

The newly installed version of Python (3.x) is installed in /Library/Frameworks/Python.framework and /usr/local/bin/python3.

In order to be able to use Python 3.x to launch scripts in a shell, the path /usr/local/bin should be added to the shell PATH. This can either be done manually, or by running the script Update Shell Profile.command located in /Applications/Python3.x/.

Using the fish shell

The script mentioned above does, unfortunately, not work for the fish shell, which I fancy. Therefore, the path must be updated manually.

The official tutorial on fish mentions how to do this. In order to prepend /usr/local/bin to PATH, modify or create the file ~/.config/fish/config.fish to include

set PATH /usr/local/bin $PATH

Installing pip

For a local Python installation, using pip to manage packages is what I prefer. Using Python 3.x > 3.4, pip comes pre-installed. To explicitly state that it works for Python 3, the program is named /usr/local/bin/pip3. Before use, pip needs to be upgraded, which is done by running

pip3 install -U pip

(Note the difference between the program's name pip3 and the argument at the end pip).

Running scripts from the Terminal

Having properly added the correct paths to PATH, a Python 3.x script is launched by

python3 ./<script_name>.py

(Note that, as with pip, a trailing '3' is added to python to explicitly state that the program is for Python 3.x).

Configure all standard Unix variables for Finder launch

In order to have correctly working Unix environment variables for Python programs launched from the Finder, the file ~/.MacOSX/environment.plist has to be created. Details can be found in Apple's Technical Document QA1067.

This is however not required if the scripts are run from the command line only!

© 2024