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#

  1. Get the env.yaml file: Download the file env.yaml and save.

  2. 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 deactivate and press Enter. 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.

  1. 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.yaml that you downloaded; otherwise see the alternative step 3 below. When you’ve navigated to the location of env.yaml, type conda env create -f env.yaml, then hit Enter. 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
  1. (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 drop env.yaml into the terminal from your browser. Then hit Enter to execute. This may take multiple minutes. After the execution has finished, you should see the output above.

  2. Activate the environment by typing conda activate pf-env into 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:

  1. Show available enviroments by typing conda info --envs. Your currently active environment (if any) will be marked by an asterisk *.

  2. Deactivate your current environment by typing conda deactivate in your (Conda Prompt) Terminal.

  3. Activate your target enviroment by typing conda activate target-env. Replace target-env with the name of your target environment.

2.2.5. (Optional reading) Backup your environment and share it with collaborators#

It is good practice to create .yaml files in which your exact environment is backed up. You can also share these with collaborators who can use the .yaml file to create an exact replica of your environment. To do so, just type conda export --file environment.yaml into your (Conda Prompt) Terminal. Be careful, this operation overrides other files called environment.yaml in your current directory.