Series: Beginner coder
Description: Introduction to virtual environments and set up for virtual beginners in research setting.

Overview

Note: Although useful for multiple applications, this post is for research settings.
Welcome! This post will explore what a virtual environment is, why its useful, and how to set it up!
Skip to tutorial

What is a virtual environment?

Python is an interpreted programming language, meaning in interpreter reads Python code line by line, and executes code. It can be thought of as an application which reads text files (with a .py extension) and performs actions accordingly. As such, Python often depends on external packages, or dependencies, to handle specific functionalities or behaviors (actually, almost all coding projects depend on code written previously by others, some may refer to this as 'standing on the shoulder of giants').

Now, depending on the specific project or analysis, you may require different packages and/or different versions of packages. For example: project A requires pyradiomics for radiology feature calculation (which itself requires Python ver 3.9) whereas project B may depend on packages which require a newer version of Python. This is where virtual environments become useful!

Virtual environments are a convenient way to manage dependencies of different projects. It provides an 'environment' where a specific versions of Python and relevant dependencies are installed for use. There are many ways of managing these virtual environments, but a convenient method is to use anaconda, a software for managing environments for data science (among other things).

How to set up an environment

Although many ways exists to manage virtual environments, the method covered here is using anaconda. Anaconda is an application used for data science which comes with many convenient tools such as the conda command. Using conda allows virtual environments to be tracked in .yaml files which lists all specific versions and packages used to fully reconstruct a given virtual environment.

Step 1 - Install anaconda

Install anaconda from the website. This is the easy part! After the installation, it will be convenient to make conda commands accessible from the command prompt or terminal. In Windows, the easiest way to do this is using the Control Panel.

  1. Press Win + S and type 'Edit the system environment variables' or 'Edit environment variables for your account'
  2. Click Environment Variables > Path > Edit
  3. Click New and type in the following paths (or where anaconda is installed)
    1. C:/Users/username/AppData/Local/anaocnda3/
    2. C:/Users/username/AppData/Local/anaocnda3/Scripts/

Step 2 - Create a virtual environment

Now that anaconda is installed, you are now free to make your virtual environments. You can start from scratch, or install a pre-built environment using a .yaml file.

The $ symbol denotes the start of your terminal. Example below.
In this case, $ = C:\Users\finaba

To create from scratch:

# Type in the following lines in your command prompt or terminal
$ conda create --name myenv
$ conda activate myenv

To create from a file:

$ conda env create -f ./path/to/env.yaml

Step 3 - Reuse a created environment

To use or ‘activate’ a virtual environment:

$ conda activate myenv

Step 4 - To save a virtual environment

To save a virtual environment into a file is quite simple:

$ conda env export > ./path/to/env.yaml

That’s all!