Chapter 2: Virtual Environments

Managing multiple environments

face Josiah Wang

If you are a software developer working on lots of projects, the main convention is to create a virtual environment for each project in the project folder itself. The aim is to have a virtual environment for each project.

Creating virtual environments elsewhere

You can also create a virtual environment anywhere on your hard drive. You just need to be able to find it easily to activate it when needed!

For example, you could dump all your virtual environments in a single folder, say ~/.venv/. This will allow you to have environments not specific to a project. Perhaps you might want a virtual environment limited to all your scientific experiments, another with just PyTorch, another with Tensorflow, another for your computer vision experiments, etc.

DO NOT create virtual environments in your home directory on machines in the Department of Computing. You might fill up your (already) limited user quota even with just a single virtual environment!

Instead, create your virtual environments in /vol/bitbucket, which is a shared space provided by CSG. You should be able to access /vol/bitbucket from any DoC machine. There is a lot of disk space here, but the files created in this directory are not backed up.

Create a subdirectory with your username, i.e. /vol/bitbucket/yourusername. You can do this with mkdir /vol/bitbucket/$USER. You can then cd into this directory, and create and activate your virtual environment as normal.

CSG has a detailed guide on installing virtual envorinments on /vol/bitbucket, including how to create a ‘shortcut’ to your virtual environment.

External tools for managing virtual environments

Instead of the built-in venv that comes with Python, you can also create and manage your environments using the virtualenvwrapper tool. This makes it easier to manage your environments, especially if you prefer to put all virtual environments in a single folder as mentioned. For example, you will be able activate a virtual environment simply with a workon my_env command. This tool does not come with Python though, so you will have to install it separately (discussed in the next chapter!)

You can also use a distribution like Anaconda, which has virtual environment management capabilities via its conda tool. Note that you might not always be able to get the latest version of a package as conda uses its own distribution system!