Python Macos Catalina

  1. Historically MacOS came preinstalled with Python 2, however starting with macOS Catalina 10.15 (released in October 2019) this is no longer the case. And since Python 2 will no longer be officially supported as of January 1, 2020, you should really use Python 3 instead. There are multiple ways to install Python 3 on a MacOS computer.
  2. This is for macOS Catalina. MacOS comes pre-installed with Python but on Catalina, this is version 2.7 which has reached the end of its life. This will create a few hiccups in the process but let’s start with the basics.
  3. Get code examples like'install python macos catalina'. Write more code and save time using our ready-made code examples.

How to setup python environment on macOS. On this page I describe how to setup python environment on macOS Catalina (10.13). When I upgrade to a next major version of macOS it's almost always some problems appear - some tools stop working, eapecially if you use your system for software development.

Pip broken under Mac OS X Catalina? TLDR;

pip install --global-option=build_ext --global-option='-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/' --global-option='-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/' --prefer-binary -r requirements.txt

As Apple has announced, Mac OS Catalina now runs under its own read only file system:

macOS Catalina runs on a dedicated, read-only system volume called Macintosh HD. This volume is completely separate from all other data to help prevent the accidental overwriting of critical operating system files. [1]

Sounds great. But if you need to change that volume, or if you use software that expects to be able to find files on that read only volume that aren't there and can't be added, you're kind of stuck.

One example is Python's pip which, for some packages, will expect to find header files located in /usr/include. Apple gave developers a get-out-of-jail-free card in Mojave:

As a workaround, an extra package is provided which will install the headers to the base system. In a future release, this package will no longer be provided. [2]

and in true Apple fashion, the package was taken away. This creates problems if you want to install commonly used, but maybe not so commonly maintained, libraries. One example is Pillow, an image processing library. This will well and truly fail to compile if you just try pip install Pillow. It looks for the headers and libraries, and doesn't find them.

What can we do? Well, for pip there's a few options. The first is simply not to compile at all, but to prefer a binary. This is as simple in some cases as the --prefer-binary option. But what if, like Pillow, there is no precompiled binary for your platform? Or for reasons of information security, you have to compile it yourself?

Fortunately, pip can be directed to look in different places using the 'global-options' flag. To get Pillow to build, use the command at the top of this article, reproduced below:

pip install --global-option=build_ext --global-option='-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/' --global-option='-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/' --prefer-binary -r requirements.txt

To understand this more fully, take a look at https://stackoverflow.com/questions/18783390/python-pip-specify-a-library-directory-and-an-include-directory.

If you need to do a similar function as part of other install tools, common environment variables are LD_LIBRARY_PATH (deprecated[3], but still commonly used), LIBRARY_PATH and INCLUDE_PATH. Setting these to the library paths (/Library ... /usr/lib) and include paths (/Library ... /usr/include) respectively may help.

[1]https://support.apple.com/en-gb/HT210650
[2]https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes#3035624
[3]https://stackoverflow.com/questions/18241517/c-include-path-vs-ld-library-path

Macos

Mac OS X comes with Python 2.7 out of the box.

You do not need to install or configure anything else to use Python 2. Theseinstructions document the installation of Python 3.

The version of Python that ships with OS X is great for learning, but it’s notgood for development. The version shipped with OS X may be out of date from theofficial current Python release,which is considered the stable production version.

Doing it Right¶

Catalina

Let’s install a real version of Python.

Before installing Python, you’ll need to install GCC. GCC can be obtainedby downloading Xcode, the smallerCommand Line Tools (must have anApple account) or the even smaller OSX-GCC-Installerpackage.

Note

If you already have Xcode installed, do not install OSX-GCC-Installer.In combination, the software can cause issues that are difficult todiagnose.

Note

If you perform a fresh install of Xcode, you will also need to add thecommandline tools by running xcode-select--install on the terminal.

While OS X comes with a large number of Unix utilities, those familiar withLinux systems will notice one key component missing: a package manager.Homebrew fills this void.

To install Homebrew, open Terminal oryour favorite OS X terminal emulator and run

The script will explain what changes it will make and prompt you before theinstallation begins.Once you’ve installed Homebrew, insert the Homebrew directory at the topof your PATH environment variable. You can do this by adding the followingline at the bottom of your ~/.profile file

If you have OS X 10.12 (Sierra) or older use this line instead

Now, we can install Python 3:

This will take a minute or two.

Pip¶

Homebrew installs pip pointing to the Homebrew’d Python 3 for you.

Working with Python 3¶

At this point, you have the system Python 2.7 available, potentially theHomebrew version of Python 2 installed, and the Homebrewversion of Python 3 as well.

will launch the Homebrew-installed Python 3 interpreter.

will launch the Homebrew-installed Python 2 interpreter (if any).

will launch the Homebrew-installed Python 3 interpreter.

Python Macos Catalina

If the Homebrew version of Python 2 is installed then pip2 will point to Python 2.If the Homebrew version of Python 3 is installed then pip will point to Python 3.

Python 2.7 Macos Catalina

Python Macos Catalina

The rest of the guide will assume that python references Python 3.

Pipenv & Virtual Environments¶

Python Version Macos Catalina

The next step is to install Pipenv, so you can install dependencies and manage virtual environments.

A Virtual Environment is a tool to keep the dependencies required by different projectsin separate places, by creating virtual Python environments for them. It solves the“Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keepsyour global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while alsomaintaining a project which requires Django 1.8.

So, onward! To the Pipenv & Virtual Environments docs!

This page is a remixed version of another guide,which is available under the same license.