In this tutorials we are going to see what is Python Virtual Environment and how to setup Python VirtualEnv in different operating systems.

Python Virtual Environment

Virtual Environment (virtual env or venv) is a tool that helps to keep the libraries and dependencies required for a project separated from others. Using this, one can install the requirements specific to a python project and prevent them from interfering with other projects.

Need for a virtualenv

Consider you are working on a project that uses the Django version 3.1 and at the same time, you want to run a Django application that uses the older 3.0 version. With time, new features are added to any package and some features become deprecated, thus, both the different versions of the applications need that particular version of Django installed to work properly, else there might be dependency issues.

Using virtual environments, one can install the packages separately for each application and keep them along with that project confined to a directory. Also, multiple environments can be activated simultaneously in different terminals.

It is a general practice to begin any python project in a venv to prevent any issues in future.

1 Creating and activating a Python virtualenv

1.1 Install virtualenv

Ensure pip is installed and added to the PATH variable and then install virtualenv using the command:

pip install virtualenv
How to setup Python VirtualEnv

1.2 Creating virtualenv

Open the terminal or Command Prompt and navigate to the directory where the project needs to be created. Run this command to create a virtualenv with the name env.

virtualenv env

You can name the virtualenv as you wish for eg. something like env, venv or project-specific: testappenv, blogappenv. 

This will create a folder with that name with all the required scripts within that.

How to setup Python VirtualEnv

1.3 Activating the virtualenv

Windows

env\Scripts\activate

Linux

source ./env/bin/activate

Once the virtualenv is activated, (env) will be visible on the starting of the command line.

1.4 Deactivate a virtualenv

After the work for the python environment is done, you can deactivate it using the command:

deactivate
activate deactivate venv

2 Sharing a virtualenv

When working on a shared project or when you want to run your project on some other machine, it is essential to install all the required version of the dependencies first. For this, pip has a feature called freeze to make a list of all the installed packages which can then be used to make a clone of the venv in some other machine.

Once in an activated virtualenv run the command:

pip freeze > requirements.txt

This will list all the dependencies for that venv and store them in a file named requirements.txt.

requirements.txt file is used for specifying what python packages are required to run the project and is supposed to be present in its root directory.

creating requirements.txt

3 Cloning a virtualenv

The requirements.txt file is used to replicate any venv. Once this file (or list of dependencies) is available:

  • Create and activate a virtualenv
  • Run this command
    pip install -r requirements.txt

    This will install all the packages as per the version number given in requirements.txt in the newly created venv.

Cloning the venv

References:

Happy Learning 🙂