Set up Python#
When using Python, we strongly recommend creating isolated environments rather than relying on any pre-installed Python installations. This provides the greatest flexibility when it comes to using different interpreters and package versions.
Several modern tools can be used to create isolated Python environments. The most common ones are:
Tool |
When it shines |
Why you might pick it |
|---|---|---|
Scientific computing, mixed Python / C libraries |
Excellent support for compiled scientific packages |
|
Projects that only depend on pure‑Python wheels |
Extremely fast resolver and installer (very lightweight) |
|
Complex, hybrid projects that need reproducible builds |
Strong focus on reproducible workflows |
Our focus here is micromamba, which offers the broadest compatibility with the scientific Python ecosystem while remaining lightweight and easy to install.
Installation#
On Linux and macOS you can install micromamba with:
"${SHELL}" <(curl -L micro.mamba.pm/install.sh)
Please refer to the official documentation for more details and different installation methods.
Creating an environment#
The recommended way to create an environment is to specify the environment name, Python version and dependencies in an environment.yaml:
name: my_env
channels:
- conda-forge
dependencies:
- python=3.12
- matplotlib
- numpy
- scipy
- eccodes
- pip:
- gribscan
Now you can create the environment by running:
micromamba env create -f environment.yaml
This approach makes it easy to share, reproduce, and recreate your environment across different machines.
Take-home messages
An example environment.yaml used at our hackathons can be found here.
Managing environments#
micromamba allows you to manage several virtual environments.
You can show the existing environments by running:
micromamba env list
The currently active environment is marked with an *.
You can switch between environments with:
micromamba activate <env_name>
Because the usage of micromamba is equivalent to that of conda, you can refer to the conda documentation for more information about managing environments.
Using an environment as IPython kernel#
To use your custom environment in Jupyter notebooks (e.g. via the DKRZ JupyterHub), you need to register it as a kernel — a bridge between Jupyter and your Python interpreter.
You can install your own Python environments as a kernel by running the following command:
micromamba activate my_env
python3 -m ipykernel install --user --name my_env
Info
The --user flag installs the kernel in the user’s local directory (safe and doesn’t require admin rights)
Afterwards, you should be able to select your environment when creating a new Jupyter notebook in the web browser.