Installing Python on MacOS and Windows

Content

Setting up Python on your Windows PC or MacOS is what we will describe in this post. In another post we will document how to finalize your installation to execute the first programs including a Junyper Notebook to generate plots of the data provided by the ECDC. The following topics are touched:

Installing a Python developing system

This document describes how to install Python on a Windows or MacOS system and use it for Python development with CVB (Windows only) and other projects.

Installing Python on a Windows system#

When you plan to use other Python components that require a special version of Python you should not install the latest version of Python. Instead install e.g. Python version 3.7.6 because the other components may have been developed for this version and the installation of so called Wheels (components for Python) have been changed in newer Python versions. In this sample we will use Python 3.7.6.

The Python downloads can be found on this site: https://www.python.org/downloads/

Select Python 3.7.6 and use the executable installer to setup your system:

Let the installer updating your PATH and select Customize installation:

Leave all options in the next dialog as default:

Use a short installation directory but ensure that you still can install different versions of Python easily:

Open a command prompt and type python -V (recognize the capital V) to check the installation, it should list your installed version:

Installing Python on MacOS#

The easiest way of installing Python is to use Homebrew to install PyEnv that will allow you to use multiple versions of Python simultaneously on your system. Homebew is some sort of Debian style APT package manager for Mac systems. You can install it by following the instructions here: https://brew.sh.

On the website you will also find a documentation of what you can do with Homebrew and it need to be said, that it is a must-have on a Mac. After installation Homebrew shows the following screen and now you are ready to install packages using brew install PackageName.

Installing PyEnv#

As a first package we will install PyEnv and using PyEnv we will install Python versions as we need them. Start a terminal, change to your user folder ( cd /User/ _ userName _) if you are not there already and execute:

brew install pyenv

This installation will take a while and the success will be shown once it is finished:

After the installation has finished, we need to modify our environment to use PyEnv in the terminal. MacOS Catalania supports the ZSH (Z shell) and the Bash shell, while earlier versions of MacOS support the Bash shell only. That's why we have to distinguish between Bash and ZSH to modify the environment.

Modifying the Bash environment#

For Bash you need to open the file .bash_profile. The file contains additional settings of your bash and it will be loaded every time you start a new bash. But the file may not exist on your system and in this case we have to create it.

Open Finder and move to the User folder. To view hidden files press + + . (shift, command and period symbol "."):

If you don't see the file called .bash_profile we have to create it by executing the following command from the terminal:

touch .bash_profile

Now the file will show up in Finder and you can open it in an editor of your choice and ensure that it includes the following lines:

export PYENV\_ROOT="$HOME/.pyenv"
export PATH="$PYENV\_ROOT/shims:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval"$(pyenv init -)"
fi

The first line sets an environment variable PYENV_ROOT to the again invisible directory called .pyenv (a leading period symbol "." makes a directory or file invisible in MacOS), in the second line we set the path to include PYENV_ROOT/shims. The next 3 lines initialize PyEnv.

Modifying the ZSH environment#

For ZSH you need to open the file .zshrc_._ The file contains additional settings of your Z shell and it will be loaded every time you start a new shell. But the file may not exist on your system and in this case we have to create it.

Open Finder and move to the User folder. To view hidden files press + + . (shift, command and period symbol "."):

If you don't see the file called .zshrc we have to create it by executing the following command from the terminal:

touch .zshrc

Now the file will show up in Finder and you can open it in an editor of your choice and ensure that it includes the following lines:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV\_ROOT/shims:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval"$(pyenv init -)"
fi

The first line sets an environment variable PYENV_ROOT to the again invisible directory called .pyenv (a leading period symbol "." makes a directory or file invisible in MacOS), in the second line we set the path to include PYENV_ROOT/shims. The next 3 lines initialize PyEnv. Keep in mind that your configuration file might be overwritten if you install ZSH extensions such as Oh-My-ZSH. In this case you have to apply the changes again.

Installing Python versions using PyEnv#

To activate the changes we did in the environment we have to restart the terminal. Use ⌘ + Q to close the terminal and restart it.

Do not install the latest version of Python. Instead install Python version 3.7.7 because the CVB python components have been developed for this version and the installation of so called Wheels (components for Python) have been changed in newer Python versions. So far the CVB Wheel does not support Python 3.8 or newer out of the box. Even when CVB does not support MacOS we want to keep both operating systems using the same version of Python so we install Python 3.7.7 using the command:

pyenv install 3.7.7

Now that Python 3.7.7 is installed through PyEnv, we want to set it as our global default version for _PyEnv_environments and we use the following command for doing that:

pyenv global 3.7.7

You can install different versions of Python using PyEnv and use pyenv global _ VersionNumber _ to switch between the different versions on the fly. For details refer to this good article:

https://opensource.com/article/20/4/pyenv.

Finally, we can use the command which python to see the path to the global Python directory:

Like in Windows we execute python -V (recognize the capital V) to check the installation, it should list your installed version (notice that I am using an older version on my Mac):