Skip to content

Virtual environments

Due to the vast amount of packages available, and different packages using different versions, we strongly recommend working in a virtual environment. In a virtual environment, your packages can be installed in an isolated environment without causing issues with packages installed in other environments. You can e.g. have one version of a package in one environment, and other versions in another environment. It is best practice to always install the packages into a virtual environment.

Some common virtual environments are pip environments, conda environments, and venv.

They are largely interchangable, but we recommend starting with pip environments, unless you have installed Python through conda/anaconda.

Pip environments

To get started, install pipenv:

pip install pipenv

Then, create a new environment (or reactivate an existing environment) by running:

pipenv shell

This will create an environment if not existing, and then launch the virtual environment shell. Note that the name of the virtual environment will be the same as the folder that pipenv shell was run from, although the name is not important.

To relaunch a virtual environment, simply go to the same directory and run pipenv shell again.

virtualenv

virtualenv is similar to pip environments, except for that the virtualenv environment is created as a subfolder in the project folder. You need to navigate into the virtual environment folder and launch the shell from there, and then go back up again.

To create a new environment you can do it in one of the following ways

  1. Using virtualenv

    First install virtualenv using pip

    pip install virtualenv
    

    Create your environment

    virtualenv "NameOfEnvironment"
    

    Note that you can specify which python you want to use with the --python option, followed by the python path.

    virtualenv --python python "NameOfEnvironment"
    
  2. Using python venv

    Alternatively, you can use venv through python, see below. This option is built in to python and you do not need to install anything extra. However, note that virtualenv is more flexible and includes more features.

    python -m venv "NameOfEnvironment"
    

A general tip could be to start the name with a dot, i.e. .Name, to easier distinguish the venv folder from your regular folders.

Then to activate the environment you need to call activate.

./NameOfEnvironment/Scripts/activate
If you want deactivate your environment you can just call deactivate

Note that in the case of using git, git is going to track the venv folder if it is in a git environment. You can add the folder to your .gitignore file to avoid this, another way is to create the virtual environment outside the git project.

You can read more about virtualenv and venv.

Conda environment

Conda environments are best when python is installed using conda/anaconda. Conda has its own repository of packages besides the ones in pip, which are typically pre-compiled. These are thus often more common when using large packages like machine learning packages.

You can read more about conda environments here.