Python Macos Catalina

I recently upgraded to macOS Catalina and it seems to come with Python 3.7.3. All of my packages have come from the PIP installed with Python 3.7.3. I have installed Python 3.7.7 (the newest vers. A clean installation of Catalina includes a /usr/bin/python3 binary, but it's a stub for installing the command line developer tools, which includes Python 3. If/when the command line developer tools are installed, the /usr/bin/python3 stub will run the actual python3 binary, but a clean install will just have the stub.

  1. Macos Catalina Python Version
  2. Macos Catalina Python 3.7
  3. Python 2.7 Macos Catalina
  4. Macos Catalina Python 3

Python has made life extremely easy for developers and data scientists, at least most of the time. One exception that ruins their life is dealing with Python versions. There would sometimes be lots of Pythons existing on a Mac at the same time. Where are they from? Are they provided by Apple? Which one am I using? Where did the packages I installed go? This article would give some insights into those questions. This article is NOT for beginners and DO NOT teach you how to install Python. It aims to give you a better understanding of all the Python interpreters you may have on your Mac.

Sure, Python is undoubtedly a programming language, but it is not only a programming language. The name 'Python' frequently used in this article would refer to the Python interpreter program most of the time. Unlike C language, which is compiled to machine code and executed by the processor directly, Python is not compiled but interpreted. It means when your Python program is running, there needs to be another program running at the same time, interpreting every line of your Python code to your processor, and the program is THE Python we are talking about in this article.

More specifically, this title means 'Which python interpreter program am I using to run my Python program?'. Most of the time, you use one of the interpreter programs with the command python or python3. However, it's good to know how many interpreters you have and which one you are currently using with the python command. Here are some ways that help you to find out which python you are using.

The type command

type command tells you how your shell resolves a command, in this case, the command python or python3. It gives the path to the program that is directly involved with a command. In my system, you would see the following outputs.

Take python as an example. It tells you /usr/bin/python is called when you use the command python. However, the path is not necessarily your interpreter's real path: it may be an alias or a trampoline. To see the actual path of the interpreter, you need other commands.

The sys.executable variable

sys.executable gives the absolute path of the executable binary for the Python interpreter you're currently using.

The sys.path variable

It's common for beginners to install the wrong interpreter package and ends up being unable to import the desired package. sys.path is a list of strings that specifies the search path for modules, and it tells where the packages you installed (typically with pip) go.

As far as I know, there is currently not an efficient way to find all Python interpreter programs that now exist on your Mac. However, I could still give the common interpreter paths you may expect on your Mac. If you come to find a way to list all interpreters or find a new Python interpreter path that is not mentioned, you are more than welcomed to share it with me.

Built-in Interpreters

Even if you don't install Python explicitly, there may be more than one Python interpreter available on your Mac already. It is not wise to remove or modify these interpreters, and you can only install packages with pip command for the user, not globally. Python you found under /usr/bin is usually system built-in interpreters, files under this path cannot be modified by users without modifying SIP, and it is strongly suggested not to do so. As far as I know, there may be 3 interpreters that are provided by the system.

  • /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7: the Python 2.7 interpreter. Almost all Macs have this interpreter, its alias is located in /usr/bin/python
  • /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7: the Python 3.7 interpreter. From macOS Catalina (reference here), the system gives this interpreter and prompts a deprecation warning when you use the system Python 2.7. It's by default /usr/bin/python3.
  • /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8: a Python interpreter installed by Xcode somehow. As far as I've observed, it overwrites /usr/bin/python3, after that, deleting the Xcode app cause /usr/bin/python3 malfunctions.

User Installed Interpreters

As the built-in interpreters may serve some system functionalities, I prefer to install another interpreter to separate my stuff from the system's interpreter. To install a Python interpreter, you can use Homebrew or install it from python.org. Here are the possible locations of the Python interpreter installed by the user.

  • /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7: the Python interpreter I installed from python.org. One of its aliases is located at /usr/local/bin/python3, and it is what my python3 command refers to.

Intro¶

Using and developing with Python on MacOS sometimes may be frustrating...

The reason for that is that MacOS uses Python 2 for its core system with pip as a package manager. When Xcode Command Line Tools are installed Python 3 and pip3 package manager will be available at the cli. When using Python2, Python3 and their package managers this way, all the packages will be installed at the system level and my effect the native packages and their dependences , this can break or lead to unwanted bugs in OS.

The right way to use python at MacOS is to use Virtual Environments for python. This way all the system related versions of python and their packages won't be affected and use by you.

Installing and configuring pyenv, pyenv-virtualenv¶

In order to use pyenv, pyenv-virtualenv without conflicting with the native MacOS python we need to add some configuration to our ~/.zshrc config (for mac os catalina) or your bash config if you are still using bash.

It's very imported to maintain the order of the configuration for the loading order

  • First of all we need to include your Executable Paths. In the example we added all the common paths, including the paths for pyenv, pyenv-virtualenv. If you have any other path that you use, you can add them at the same line or create a new line below this one.
  • Second to Executable Paths we will add two if statements that will check if the pyenv,pyenv-virtualenv are installed, if they are it will load them. If they aren't and you are using the same zsh or bash config it will ignore loading them
  • Third is a fix for brew, brew doctor. When using this method it may conflict with brew as it uses python as well. If you run run brew doctor without the fix, it will show config warnings related to the python configuration files.

Macos Catalina Python Version

Configuration for ~/.zshrc or ~/.zprofile

After you saved your configuration the best way to load it is to close your terminal session and open it again. This will load the session with your updated configuration. There should be no errors at the new session.

This will install both pyenv and pyenv-virtualenv

Test if pyenv loaded currently

After the installation we would like to set a system level python version, you can chose the default from the list available from the pyenv

List available Python Version and find the version suited for your needs:

Install Requeued Python Version (Exmaple version 3.9.5) as a default system

Set it as global

Python

You can install multiply versions of python at the same time.

List all installed python versions and virtual environments and their python versions

Now let's test our system Python version we set before, it should be the version you choose as Global before

Macos Catalina Python 3.7

So far we cleaned your system and installed and configured pyenv, pyenv-virtualenv.

Macos

How to use pyenv-virtualenv¶

Catalina

Now let's understand how to use Python Virtual Environment with pyenv-virtualenv

Macos

Full documentation can be found at the original repo at git hub: pyenv-virtualenv github

We will list here some basic examples for a quick start and basic understanding

To create a virtualenv for the Python version used with pyenv, run pyenv virtualenv, specifying the Python version you want and the name of the virtualenv directory. For example,

This will create a virtualenv based on Python 3.9.5 under $(pyenv root)/versions in a folder called my-project-name

Activating virtualenv automatically for project

The best way we found to activate the virtualenv at your project is to link the projects directory to the virtualenv.

cd to the project's directory and link the virtualenv for example my-project-name virtualenv

This will activate the linked virtualenv every time you cd to this directory automatically From now you can use pip to install any packages you need for your project, the location of the installed packages will be at $(pyenv root)/versions/

Activating virtualenv manually for project

You can also activate and deactivate a pyenv virtualenv manually:

This will alow you to use multiply versions of python or packages for the same project

List existing virtualenvs

Delete existing virtualenv

Python 2.7 Macos Catalina

or

You and your MacOS should be ready for using python the right way without conflicting any system or Xcode Command Line Tools (used by brew)

Macos Catalina Python 3

Comments