Environments#

It is often necessary to retain two different but fully functional package specifications. Imagine having two courses A and B. One has the requirement of numpy==1.23 while the other has numpy==1.25. In cases where these are incompatible, for instance an API change, one would be required to change the installed packages when participating in each course.

Instead, one can use environments which allows one to retain a fully functional package list and easily swap between them.

Note

Using environments can be used for a variety of things:

  1. quickly test whether certain packages conflicts

  2. check whether a developed Python script is compatible with different package versions.

  3. ensures a minimal package list (no unused packages)

  4. have multiple functional package environments

It is our clear recommendation to create an environment for each course. In this way you will ensure that anything you do in that course environment will not interfere with your other course environments.

venv – environment #

venv – VS Code#

VS Code manages virtual environments via intrinsic VS Code commands.

Here is a brief explanation on how to setup a virtual environment in VS Code.

Warning

If you encounter problems with virtual environments in VS Code, please see here.

First check that you are not already using a virtual environment (see point 5. below).

  1. Create a folder/directory according to the course you wish to work on.

    This is optional, but recommended to retain a structure of your course activities.

  2. Press Open Folder to start working in the designated course folder. If you already have several opened folders, this will not be visible. Just select the workspace.

    Open folder in VS Code
  3. Once you are in a folder/workspace we should create a virtual environment. Press Ctrl-Shift-P and search for Python: Create Environment..., and select that.

    VS Code command prompt
  4. Next, select the below option

    VS Code venv creation
  5. Check that the virtual environment is functional. Sometimes you need to run a small Python script to enable the virtual environment, either press , or Shift-Enter

    Check if virtual environment is enabled through VS Code

venv – Terminal#

The package venv creates environments using the pip method. Once an environment has been created, and sourced, all pip commands only act in that environment.

Note

The venv package can only create environments with different packages. It cannot switch between different Python versions.

An environment will be a directory on your computer. Deleting it will be the same as deleting the environment.

Warning

Environments in Visual Studio Code requires special handling when working in workspaces. Please see here.

An environment can be created using:

python -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python -m venv course-A
python -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python -m venv course-A
python3 -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python3 -m venv course-A
python3 -m venv <path to venv>
# create an environment named 'course-A' in the current directory
python3 -m venv course-A

Once created, one can activate/use the environment by sourcing a file. Change course-A with the name of the environment.

Warning

Please see this FAQ entry!

course-A\Scripts\Activate.ps1

Warning

Please see this FAQ entry!

course-A\Scripts\activate.bat
source course-A/bin/activate
source course-A/bin/activate

Now every executed Python script will only use the packages installed in the environment. And every pip command will only install/remove packages in the environment. To get out of the environment, simply run the command deactivate.

Below is a complete example of creating an environment, installing a specific package, running a code using the environment, and getting out of it.

python -m venv numpy-env
numpy-env\Scripts\Activate.ps1
pip install "numpy==1.23.*"
python -c "import numpy as np ; print(np.__version__)"
deactivate
python -m venv numpy-env
numpy-env\Scripts\activate.bat
pip install "numpy==1.23.*"
python -c "import numpy as np ; print(np.__version__)"
deactivate
python3 -m venv numpy-env
source numpy-env/bin/activate
pip install "numpy==1.23.*"
python3 -c "import numpy as np ; print(np.__version__)"
deactivate
python3 -m venv numpy-env
source numpy-env/bin/activate
pip install "numpy==1.23.*"
python3 -c "import numpy as np ; print(np.__version__)"
deactivate

conda – environment#

The conda Python package automatically incorporates environments. Not only will it allow environments with specific packages, one can also create environments with different Python versions. This can be useful if a specific package requires a specific Python version.

Once an environment has been created, and sourced, all python commands only act in that environment.

An environment can be created using:

Note

One can list the available global environments (created with --name) by executing conda env list.

conda create --name <name of env>
# create an environment named 'course-A' (in the global environment list)
conda create --name course-A
# alternatively the environment can be placed in a sub directory
conda create --prefix course-A
conda create --name <name of env>
# create an environment named 'course-A' (in the global environment list)
conda create --name course-A
 # alternatively the environment can be placed in a sub directory
conda create --prefix course-A
conda create --name <name of env>
# create an environment named 'course-A' (in the global environment list)
conda create --name course-A
# alternatively the environment can be placed in a sub directory
conda create --prefix course-A
conda create --name <name of env>
# create an environment named 'course-A' (in the global environment list)
conda create --name course-A
# alternatively the environment can be placed in a sub directory
conda create --prefix course-A

Once created, one can use the environment by activating it. Change course-A with the name of the environment.

# if the enviroment was created with --name:
conda activate course-A
# if the environment was created in a sub directory (--prefix):
conda activate .\course-A
# if the enviroment was created with --name:
conda activate course-A
# if the environment was created in a sub directory (--prefix):
conda activate .\course-A
# if the enviroment was created with --name:
conda activate course-A
# if the environment was created in a sub directory (--prefix):
conda activate ./course-A
# if the enviroment was created with --name:
conda activate course-A
# if the environment was created in a sub directory (--prefix):
conda activate ./course-A

Now every executed Python script will only use the packages installed in the environment. Every conda install will only install/remove packages in the environment. To get out of the environment, simply run the command conda deactivate.

Below is a complete example of creating an environment using a specific Python version, installing a specific package, running a code using the environment, and getting out of it.

conda create --name numpy-env python=3.10
conda activate numpy-env
conda install "numpy=1.23"
python -c "import numpy as np ; print(np.__version__)"
conda deactivate
conda create --name numpy-env python=3.10
conda activate numpy-env
conda install "numpy=1.23"
python -c "import numpy as np ; print(np.__version__)"
conda deactivate
conda create --name numpy-env python=3.10
conda activate numpy-env
conda install "numpy=1.23"
python3 -c "import numpy as np ; print(np.__version__)"
conda deactivate
conda create --name numpy-env python=3.10
conda activate numpy-env
conda install "numpy=1.23"
python3 -c "import numpy as np ; print(np.__version__)"
conda deactivate

virtualenv – environment#

The package virtualenv creates environments using the pip method. Once an environment has been created, and sourced, all pip commands only act in that environment.

Install it with pip using the package name virtualenv.

virtualenv is the original implementation of the venv package. virtualenv will be continuously developed and contains more up to date methods and functionalities. As such virtualenv can be updated while venv is shipped with the Python sources and thus can not be updated.

The usage of virtualenv is equivalent to venv by replacing all venv entries with virtualenv. Additional options can be found on the homepage.

Jupyter Notebooks / IPython#

Virtual environments and Jupyter can cause issues when Jupyter is installed in the system (not in the virtual environment). This is because the kernel is fixed to launch the Python interpreter it got installed with.

Warning

This issue will arise in any virtual environment, including conda.

The simplest way to check whether your Jupyter Notebook is using your virtual environment is to execute the following code in a notebook cell:

import sys
print(sys.exec_prefix)

if it shows a directory where you have your virtual environment, you are all set! If not, then the simplest solution would be to install the kernel runner in the virtual environment:

python -m ipykernel install --prefix <path to venv|conda-env>
python -m ipykernel install --prefix <path to venv|conda-env>
python3 -m ipykernel install --prefix <path to venv|conda-env>
python3 -m ipykernel install --prefix <path to venv|conda-env>

Only insert the folder name in the < ... > block. The problem, and fix, is described in greater detail here.