Create and Activate a Virtual Environment - Python Development

Documented by kbruder tech✞ᵀᵀᴹ on Oct 08, 2020
Last updated on Mar 07, 2021


Python Logo

Introduction

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.


  1. Install and update Pip

    Python Package Installer is used to find download and update python modules. Download it for free on the command line

    Tip: If you already have Python 3 and Pip installed and you do not wish to create a new user, skip to step 4.

    $ sudo apt update -y && sudo apt install python3-pip -y
    $ pip3 install update pip
    

  2. Create a Virtual Environment User

    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.

    Tip: You can skip to step 4 if you do not wish to create a system user for your virtual environment.

    $ sudo useradd -m -r -s /bin/bash lookaway
    

  3. Switch to the New User

    Use the following command to switch users

    $ sudo su - lookaway
    

  4. Install virtualenv

    Use the following command to install virutalenv.

    $ pip3 install virtualenv --user
    

  5. Create a New Virtual Environment

    Use the following command to create a new environment.

    $ ~/.local/bin/virtualenv lookaway-env
    

  6. Activate the Virtual Environment

    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/
    

  7. Update Your Requirements

    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
    

Conclusion

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.


Referenced by

Lookaway CMS - Open Source Content Management System

Lookaway CMS is an open source content management system for independent publishing. Built with Python and Django.

Lookaway CMS Production Server - Ubuntu 20

Learn how to deploy Lookaway CMS onto a public webserver using Ubuntu 20.04 and PostgreSQL 10.