2.2. Setting up Conda environments#
2.2.1. Why should you use a Conda environment?#
Python is available in multiple different versions. Newer releases (for example, Python 3.13.8) include features that are not present in older versions (such as Python 2.6.7). To follow the contents of this course, it is recommended to use Python version 3.13 or later. Additionally, later chapters require Python packages that not included by default in the Python installation (Miniconda, previous page). In order to avoid installation of each separate package on its own, we will make use of Conda environments. These environments specify the Python version and the Python packages available to your Python scripts. This helps ensure that your Python scripts produce consistent results across different systems. Check out also the glossary entry on environments.
It is good practice to create a separate environment for each Python project you work on. To follow the book, you will only need one environment (we will name it pf-env for “Programming Foundations environment”), the setup for which is explained below. Check out also the glossary entry on environments.
2.2.2. Video Tutorials#
The following instructional videos provide visual step-by-step guidance for setting up a Conda environment on different operating systems. They complement the written instructions below and can help you follow along more easily during the setup process.
Instructional video for Windows.
Instructional video for macOS.
2.2.3. Creating a new Conda environment#
Get the env.yaml file: Download the file env.yaml and save.
Get Conda ready to create a new environment. The following procedure makes sure that Conda is in the right state to create new environments:
Open your Terminal (macOS) or Conda Prompt Terminal (WindowsOS; note that it may be called differently for you - common names are Anaconda Prompt, Miniconda Prompt, Miniconda3 Prompt, Miniforge Prompt).
Type
conda deactivateand pressEnter. Should any environment already be active, it is deactivated by this instruction.Type
conda activate base. This activates the base environment that was automatically created when you installed Conda. Next to your path (text before your cursor in the terminal) it should now say(base), which indicates that you are in the base environment.
Create a new environment from a file. If you’re already familiar with terminal commands, in your (Conda Prompt) Terminal navigate to the folder containing the file
env.yamlthat you downloaded; otherwise see the alternative step 3 below. When you’ve navigated to the location ofenv.yaml, typeconda env create -f env.yaml, then hitEnter. This may take multiple minutes. After execution has finished, you should see the following:
#
# To activate this environment, use
#
# $ conda activate pf-env
#
# To deactivate an active environment, use
#
# $ conda deactivate
(Alternative) If you cannot find the location of env.yaml on your computer, you can also create the environment by typing
conda env create -f(with space at the end) and drag and dropenv.yamlinto the terminal from your browser. Then hitEnterto execute. This may take multiple minutes. After the execution has finished, you should see the output above.Activate the environment by typing
conda activate pf-envinto your (Conda Prompt) Terminal. Now, next to your path (before your cursor) it should say(pf-env)in your (Conda Prompt) terminal.
2.2.4. (Optional reading) Switching to a different Conda environment#
When working on multiple projects, it is wise to have an isolated environment for each. To switch between environments, follow the procedure below:
Show available enviroments by typing
conda info --envs. Your currently active environment (if any) will be marked by an asterisk*.Deactivate your current environment by typing
conda deactivatein your (Conda Prompt) Terminal.Activate your target enviroment by typing
conda activate target-env. Replacetarget-envwith the name of your target environment.