Documented by Kyle Bruder on Oct 08, 2020
Last updated on Mar 07, 2021
Because Python projects often have specific module requirements, we need to ensure that these modules are installed on the systems on which they run and are developed. A virtual environment is a way to make sure you are developing, testing and running your project in the same environment no matter which system.
Think about when you take a trip away from home. You bring a bag with your clothes, chargers and everything else you need to use everyday no matter where you happen to be. Imagine if you could clone your house and be able to enter it through a magic door no matter where you are. You wouldn't need to bring your bag with you, and if you forgot to bring something important, it wouldn't matter because you could just use your magic door to access the things in your home.
In this analogy, you are the code, your house is the virtual environment, the things you need in your house are the modules and that magic door is the "activate" file in your virtual environment. Virtual environments can use a list of required modules called "requirements.txt". As modules are added to the project they are added to the requirements file. When the environment is needed on a new system, it is created on the fly and the requirements.txt is used to determine which modules to include.
Python Package Installer is used to find download and update python modules. Download it for free on the command line
$ sudo apt update -y && sudo apt install python3-pip -y $ pip3 install update pip
It is a good idea to create an account for each virtual environment. This is not required, but things can get messy when you have multiple environments being used by the same account especially when more than person is using the same user account. The whole point of Unix type systems is to allow multiple users to use a server at the same time. Users are cheap in Linux so there is no good reason not to have a different user account for each virtual environment if you have sudo access. In this example, we are creating a system level account (-r) with a bash shell (-s /bin/bash) and the default home directory (-m) called lookaway.
$ sudo useradd -m -r -s /bin/bash lookaway
Use the following command to switch users
$ sudo su - lookaway
Use the following command to install virutalenv.
$ pip3 install virtualenv --user
Use the following command to create a new environment.
$ ~/.local/bin/virtualenv lookaway-env
Use the following commands to activate the environment and change to the base directory of the environment. Once it is active, your prompt will change to include the name of your environment. Notice the (lookaway-env) in the example below.
$ source ~/lookaway-env/bin/activate (lookaway-env) $ cd ~/lookaway-env/
Each time you change the modules installed in your virtual environment, don't neglect to update your requirements.txt file. Other people using your software and future you will appreciate it!
(lookaway-env) $ pip freeze > requirements.txt
Now you have created and activated your very own virtual environment. Which ever versions of Python modules you install or update in here will not affect other virtual environments or the host system.
Learn how to deploy Lookaway CMS onto a public webserver using Ubuntu 20.04 and PostgreSQL 10.
Lookaway CMS is an open source content management system for independent publishing. Built with Python and Django.