Hi, I'm Brett

Marketing Specialist

The Power of the Python Virtual Environment


People often come to me asking “how do I add x” to my system… or say “it’s missing dependency x…” but in reality, the user has actually failed to look at the venv module to help with their day to day operations.

Virtual environments are a way for Python programs to be run in a somewhat isolated environment. Dependencies are stored as part of the environment instead of adding unnecessary bloat to the host, so when you go and delete that instance of whatever software with god-only-knows how many deps, you can rest assured that they’re gone.

Getting started with Virtual Environments is quite simple, first, you need to install the version of python that is required, (you may need to use pyenv if it’s something obscure), and ensure that the venv module is installed. From here, you can create the new virtual environment (I’ll just be using the os install for this guide), run the following commands.

python3 -m venv myvirtualenvironment

This will create a new virtual environment using the system installed version of python in a folder called myvirtualenvironment. If you’re working in userland, it’d be smart to create a folder like .venv in the home dir to not clutter it, then make a new venv per application. What is truly important is that you need to make sure that your application is using the venv, so when starting it you need to add something like $HOME/.venv/myapp/bin/python3 myapp.py as your runner, as with any kind of dependency installation. If you do anything as root, make sure that you’re chowning things to the correct user/group that you’re going to be working as.