411.
069 Special Topics Data Handling and Programming in
Climate Science
05. Python (Virtual) Environments
Florian Ladstädter1
2025-10-14
1 Wegener Center and IGAM/Institute of Physics,
University of Graz, Austria
1/20
Data Handling and Programming in Climate Science
Python
(Virtual)
Environments
2/20
Data Handling and Programming in Climate Science
Python Virtual Environments – Why?
• Python comes with a set of modules, the “standard lib”. Applications will almost
always need additional libraries.
3/20
Data Handling and Programming in Climate Science
Python Virtual Environments – Why?
• Python comes with a set of modules, the “standard lib”. Applications will almost
always need additional libraries.
• Different applications might need different versions of these libraries. Each library
might depend on specific versions of other libraries. This creates version conflicts.
3/20
Data Handling and Programming in Climate Science
Python Virtual Environments – Why?
• Python comes with a set of modules, the “standard lib”. Applications will almost
always need additional libraries.
• Different applications might need different versions of these libraries. Each library
might depend on specific versions of other libraries. This creates version conflicts.
• There is a large amount of libraries, and a huge number of version combinations.
3/20
Data Handling and Programming in Climate Science
Python Virtual Environments – Why?
• Python comes with a set of modules, the “standard lib”. Applications will almost
always need additional libraries.
• Different applications might need different versions of these libraries. Each library
might depend on specific versions of other libraries. This creates version conflicts.
• There is a large amount of libraries, and a huge number of version combinations.
• The solution to this mess is to use virtual environments to isolate each of your
Python projects and applications, and a package manager.
3/20
Data Handling and Programming in Climate Science
Python Virtual Environments – Why?
• Python comes with a set of modules, the “standard lib”. Applications will almost
always need additional libraries.
• Different applications might need different versions of these libraries. Each library
might depend on specific versions of other libraries. This creates version conflicts.
• There is a large amount of libraries, and a huge number of version combinations.
• The solution to this mess is to use virtual environments to isolate each of your
Python projects and applications, and a package manager.
• A “virtual environment” is a separated directory tree containing a Python
installation and libraries in specific versions.
3/20
Data Handling and Programming in Climate Science
Python Environments
To use Python, you need:
1. A Python installation ([Link]
4/20
Data Handling and Programming in Climate Science
Python Environments
To use Python, you need:
1. A Python installation ([Link]
2. A Python package manager to install and organize Python packages;
4/20
Data Handling and Programming in Climate Science
Python Environments
To use Python, you need:
1. A Python installation ([Link]
2. A Python package manager to install and organize Python packages;
3. A way to create and organize separate “environments” to isolate the Python
packages used for each Python project (virtual environments).
4/20
Data Handling and Programming in Climate Science
Python Environments
Two basic options:
1. conda package and environment manager—provides package, dependency, and
environment management for any language; use it with miniforge and mamba.
Also installs the interpreter (Python in our case).
5/20
Data Handling and Programming in Climate Science
Python Environments
Two basic options:
1. conda package and environment manager—provides package, dependency, and
environment management for any language; use it with miniforge and mamba.
Also installs the interpreter (Python in our case).
2. pip and venv—pip is the package manager, and venv is the officially
recommended way to create virtual environments. Both are included in Python.
(Note: pip and venv are two independent tools, but mentioned together here to
match conda functionality)
For point 2, many additional tools exist with different focuses, e.g. uv, virtualenv,
virtualenvwrapper, poetry, pyenv, pipx, or pipenv. These tools are not necessary for working with
virtual environments, but make work more convenient/powerful.
5/20
Data Handling and Programming in Climate Science
Python Environments
conda:
+ integrated package and environment manager for any language, not only Python
+ can mix tools from different languages
6/20
Data Handling and Programming in Climate Science
Python Environments
conda:
+ integrated package and environment manager for any language, not only Python
+ can mix tools from different languages
− comes from a time when Python package management was not working well
− not specific to Python, bloated
− even the here-recommended package server/channel conda-forge misses packages
6/20
Data Handling and Programming in Climate Science
Python Environments
pip/venv:
+ Python build-in, officially recommended
+ lightweight
+ flexible, many similar tools available enhancing the capabilities
+ pip’s package server PyPI is the most complete and up-to-date
+ pip (or related tools) is what is most commonly used
+ pip is the tool we will need for our software projects
7/20
Data Handling and Programming in Climate Science
Python Environments
pip/venv:
+ Python build-in, officially recommended
+ lightweight
+ flexible, many similar tools available enhancing the capabilities
+ pip’s package server PyPI is the most complete and up-to-date
+ pip (or related tools) is what is most commonly used
+ pip is the tool we will need for our software projects
− venv gives you some flexibility, you need to take care about proper usage yourself
7/20
Data Handling and Programming in Climate Science
conda: anaconda, miniconda, miniforge, mamba, conda-forge?
8/20
Data Handling and Programming in Climate Science
conda: anaconda, miniconda, miniforge, mamba, conda-forge?
• anaconda is a Python distribution. It is quite popular, and made the conda
package manager popular at a time when Python dependency management was a
problem.
8/20
Data Handling and Programming in Climate Science
conda: anaconda, miniconda, miniforge, mamba, conda-forge?
• anaconda is a Python distribution. It is quite popular, and made the conda
package manager popular at a time when Python dependency management was a
problem.
• anaconda is a commercial company, and they changed their terms of service
in 2020. Still “free” for individuals, non-commercial usage
([Link]
8/20
Data Handling and Programming in Climate Science
conda: anaconda, miniconda, miniforge, mamba, conda-forge?
• anaconda is a Python distribution. It is quite popular, and made the conda
package manager popular at a time when Python dependency management was a
problem.
• anaconda is a commercial company, and they changed their terms of service
in 2020. Still “free” for individuals, non-commercial usage
([Link]
• anaconda uses its own distribution channel main/defaults for (Python)
packages. Often the packages there are outdated, or missing, or patched by
anaconda for nontransparent reasons.
8/20
Data Handling and Programming in Climate Science
conda: anaconda, miniconda, miniforge, mamba, conda-forge?
• anaconda is a Python distribution. It is quite popular, and made the conda
package manager popular at a time when Python dependency management was a
problem.
• anaconda is a commercial company, and they changed their terms of service
in 2020. Still “free” for individuals, non-commercial usage
([Link]
• anaconda uses its own distribution channel main/defaults for (Python)
packages. Often the packages there are outdated, or missing, or patched by
anaconda for nontransparent reasons.
• miniconda is a minimal installation for the large anaconda project. We don’t
need anaconda with (maybe too) many options and packages. With targeted
virtual environments, a minimal installation is preferred.
8/20
Data Handling and Programming in Climate Science
anaconda, miniconda, miniforge, mamba, conda-forge?
• conda-forge is a community-driven conda-channel, often more up-to-date, more
complete, and unmodified, compared to anaconda’s channel → recommended.
9/20
Data Handling and Programming in Climate Science
anaconda, miniconda, miniforge, mamba, conda-forge?
• conda-forge is a community-driven conda-channel, often more up-to-date, more
complete, and unmodified, compared to anaconda’s channel → recommended.
• miniforge is the Python distribution from the community of conda-forge, and
provides a Python solution without the ties to the commercial anaconda:
1. Uses conda-forge as default channel to receive packages, instead of the standard
anaconda channel.
9/20
Data Handling and Programming in Climate Science
anaconda, miniconda, miniforge, mamba, conda-forge?
• conda-forge is a community-driven conda-channel, often more up-to-date, more
complete, and unmodified, compared to anaconda’s channel → recommended.
• miniforge is the Python distribution from the community of conda-forge, and
provides a Python solution without the ties to the commercial anaconda:
1. Uses conda-forge as default channel to receive packages, instead of the standard
anaconda channel.
2. Uses mamba instead of conda as the package install tool. conda is older and
therefore more prominent. mamba works like conda, but is much faster (conda can
be very slow).
9/20
Data Handling and Programming in Climate Science
1.a Python Environments: miniforge/mamba [Windows]
[Link]
1. Go to the download page:
[Link]
2. From the Download section, download the installer for your operating system and
architecture (x86_64 will usually be the right choice).
3. Run the installer, and choose the recommended settings.
4. Open a “miniforge prompt”, and type mamba list to test the installation. A list of
packages names should appear.
10/20
Data Handling and Programming in Climate Science
1.b Python Environments: miniforge/mamba [Linux/MacOS/WSL]
[Link]
1. For Linux/MacOS and for WSL, follow [Link]
tab=readme-ov-file#unix-like-platforms-macos-linux--wsl:
Agree to the license notes with “yes”, then you can accept the defaults, except for the last
question about “running conda init?”. Here, choose “yes”.
11/20
Data Handling and Programming in Climate Science
1.b Python Environments: miniforge/mamba [Linux/MacOS/WSL]
[Link]
2. Open a new terminal. Your prompt should now look similar to:
(base) username@computer:~$
This indicates that we are now in the base environment.
3. Type mamba list to test the installation. A list of packages names should appear.
4. Install bash completion (Linux only): mamba install mamba-bash-completion
12/20
Data Handling and Programming in Climate Science
Working with virtual environments–conda
1. Create virtual environment with name “myproject”
mamba create --name myproject python (includes Python)
or: mamba create --name myproject python <packages> (installing Python and <packages>)
Never install your project dependencies into the base environment!!
13/20
Data Handling and Programming in Climate Science
Working with virtual environments–conda
1. Create virtual environment with name “myproject”
mamba create --name myproject python (includes Python)
or: mamba create --name myproject python <packages> (installing Python and <packages>)
Never install your project dependencies into the base environment!!
2. Activate, deactivate, install
• Activate: mamba activate myproject
• Deactivate: mamba deactivate
• Install packages, e.g.: mamba install ipython numpy matplotlib
13/20
Data Handling and Programming in Climate Science
Working with virtual environments–conda
1. Create virtual environment with name “myproject”
mamba create --name myproject python (includes Python)
or: mamba create --name myproject python <packages> (installing Python and <packages>)
Never install your project dependencies into the base environment!!
2. Activate, deactivate, install
• Activate: mamba activate myproject
• Deactivate: mamba deactivate
• Install packages, e.g.: mamba install ipython numpy matplotlib
3. Misc.
• List all envs: mamba env list
• Install a specific version: mamba install numpy=1.23
• Delete an environment: mamba env remove -n myproject
• Update everything in active environment: mamba update --all 13/20
Data Handling and Programming in Climate Science
2. Python Environments: pip/venv
1. Install Python: [Link]
2. Package manager pip and virtual environment manager venv are part of Python;
just use it.
14/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
1. Create virtual environment with name “myproject”
python -m venv myproject
15/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
1. Create virtual environment with name “myproject”
python -m venv myproject
2. Activate, deactivate, install
• Activate: myproject\Scripts\activate (Windows)
• Activate: source myproject/bin/activate (Linux)
• Deactivate: deactivate
• Install packages, e.g.: python -m pip install ipython numpy matplotlib
• Update package, e.g.: python -m pip install --upgrade matplotlib
15/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
1. Create virtual environment with name “myproject”
python -m venv myproject
2. Activate, deactivate, install
• Activate: myproject\Scripts\activate (Windows)
• Activate: source myproject/bin/activate (Linux)
• Deactivate: deactivate
• Install packages, e.g.: python -m pip install ipython numpy matplotlib
• Update package, e.g.: python -m pip install --upgrade matplotlib
3. Misc.
• Install a specific version: python -m pip install numpy==1.23
15/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
venv—Decide where to create your environment directories/folders
• A Python virtual environment is just a directory structure (for both conda and
venv)
• For venv, two common options to place the venv directory are:
16/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
venv—Decide where to create your environment directories/folders
• A Python virtual environment is just a directory structure (for both conda and
venv)
• For venv, two common options to place the venv directory are:
1. Inside each Python project directory:
Go to your Python project directory and use python -m venv myproject
Note: You need to take care that the venv directory is not part of git (.gitignore).
16/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
venv—Decide where to create your environment directories/folders
• A Python virtual environment is just a directory structure (for both conda and
venv)
• For venv, two common options to place the venv directory are:
1. Inside each Python project directory:
Go to your Python project directory and use python -m venv myproject
Note: You need to take care that the venv directory is not part of git (.gitignore).
2. At a common directory, e.g. in your home directory under the common directory
venvs or .venvs:
python -m venv C:\USERS\USERNAME\venvs\myproject (Windows)
python -m venv ~/.venvs/myproject (Linux)
With this you have all virtual environments in one place and it’s easy to keep an
overview. 16/20
Data Handling and Programming in Climate Science
Working with virtual environments–venv
venv—Summary (Linux)
• Create venv: python -m venv ~/.venvs/myproject
(Note: In your system, the python binary might be called python3; then, to
create the venv you need to use python3 -m venv <name> instead!)
• Activate venv: source ~/.venvs/myproject/bin/activate
• Install package: python -m pip install xarray
• Deactivate venv: deactivate
17/20
Data Handling and Programming in Climate Science
Some notes
• Use a dedicated virtual environment for each Python project.
• Treat each virtual environment as disposable.
18/20
Data Handling and Programming in Climate Science
Some notes
• Use a dedicated virtual environment for each Python project.
• Treat each virtual environment as disposable.
• Use the features of your IDE! E.g. VSCodium or pycharm have build-in
functionality to work with virtual environments.
18/20
Data Handling and Programming in Climate Science
Some notes
• Use a dedicated virtual environment for each Python project.
• Treat each virtual environment as disposable.
• Use the features of your IDE! E.g. VSCodium or pycharm have build-in
functionality to work with virtual environments.
• Recommended reads:
• Very short tutorial to venv and pip:
[Link]
• After reading the following, all questions regarding virtual envs will be answered:
[Link]
18/20
Data Handling and Programming in Climate Science
Some notes – go beyond
• Use virtualenvwrapper to make working with venv comfortable and smooth
([Link]
• Have a look at poetry, and at
[Link] if
you are interested in a more advanced setup/usage.
• Use uv, the newest kid in town. It replaces pip, venv, poetry, and a lot of other
tools, and is orders of magnitude faster ([Link] Quite
probably the future (and present) of Python package and project managing.
19/20
Data Handling and Programming in Climate Science
Time for Exercises–Setup your (virtual) environment Use a Linux shell (WSL) for
all tasks.
1. Create a virtual environment called, e.g., test_env.
2. Activate the environment. Check how your prompt looks like now.
3. Install the package matplotlib in the virtual environment.
4. Install the package numpy in version 1.26.2.
5. Install ipython, run ipython, and enter the following on the ipython shell:
import this
6. Install jupyter lab in a dedicated environment and open a notebook by typing
jupyter lab.
20/20