Python environments

Interpreters

To install different Python interpreters I strongly recommend asdf1.

Let's look at to install Python in two different OSes, macOS and Fedora.

macOS

To install asdf on a macOS, first install the general dependencies with

$ brew install coreutils curl git

then install asdf itself with

$ brew install asdf

Add to the shell, in our case zsh with:

$ echo -e "\n. $(brew --prefix asdf)/asdf.sh" >> ~/.zshrc

Add a plugin, in our case Python, with

$ asdf plugin add Python

You can list all available versions with

$ asdf list all Python

Install a specific version, say,

$ asdf install Python 3.9.0

Fedora

To install asdf on a Fedora, first install the general dependencies

$ sudo dnf install curl git

Clone the repository

$ git clone https://github.com/asdf-vm/asdf.git \
  ~/.asdf --branch v0.8.0

Add to zsh with

$ . $HOME/.asdf/asdf.sh`

pyenv

Compiling on macOS

pyenv can be notoriously problematic on macOS. For instance, running pyenv doctor on my laptop2 will result in:

Cloning ~/.pyenv/plugins/pyenv-doctor/bin/.....
Installing python-pyenv-doctor...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 10.15.7 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/c2/9d2fsqt57t10zn1f2ylp1jxw0000gn/T/python-build.20210128094523.17091
Results logged to /var/folders/c2/9d2fsqt57t10zn1f2ylp1jxw0000gn/T/python-build.20210128094523.17091.log

Last 10 log lines:
checking readline/readline.h, presence... no
checking for readline/readline.h,... no
checking readline/rlconf.h usability... yes
checking readline/rlconf.h presence... yes
checking for readline/rlconf.h... yes
checking for SSL_library_init in -lssl... no
configure: WARNING: OpenSSL <1.1 not installed. Checking v1.1 or beyond...
checking for OPENSSL_init_ssl in -lssl... no
configure: error: OpenSSL is not installed.
make: *** No targets specified and no makefile found.  Stop.
Problem(s) detected while checking system.

See https://github.com/pyenv/pyenv/wiki/Common-build-problems for known solutions.

The problem in this case is that pyenv can't find the relevant C headers for compilation of new versions. This can be fixed by using:

$ CFLAGS="-I$(brew --prefix openssl)/include \
  -I$(brew --prefix readline)/include \
  -I$(xcrun --show-sdk-path)/usr/include" \
  LDFLAGS="-L$(brew --prefix openssl)/lib \
  -L$(brew --prefix readline)/lib \
  -L$(xcrun --show-sdk-path)/usr/lib" \
  pyenv doctor

and the output will be:

Cloning ~/.pyenv/plugins/pyenv-doctor/bin/.....
Installing python-pyenv-doctor...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed python-pyenv-doctor to /var/folders/c2/9d2fsqt57t10zn1f2ylp1jxw0000gn/T/pyenv-doctor.20210128095003.18889/prefix

Congratulations! You are ready to build pythons!

Poetry

Poetry as Jupyter kernel

To register a poetry environment (named foo) as a Jupyter kernel, run:

poetry run python -m ipykernel install --user --name foo

venv

Create a new venv with the command:

$ virtualenv venv

and activate it using (under Bash or zsh) with:

$ source venv/bin/activate

Anaconda

First download Anaconda (or Miniconda). Once installed you can proceed to create environments3.

Creating environments

An environment foo can be created using

conda create --name foo

One it is created, it can be activated using

conda activate foo

  1. https://asdf-vm.com/ 

  2. I'm running Big Sur at the moment of writing 

  3. The remainder will assume that you have installed Anaconda, rather than Miniconda