DRF: Virtual Environment and Environment Variables

Manish Sharma
3 min readJul 8, 2023

--

DRF: Virtual Environment and Application Configuration
DRF: Virtual Environment and Application Configuration

This is part-5 of the DRF/Django Tutorial series.

All tutorials: Part-1 Part-2 Part-3 Part-4 Part-5 Part-6

It is a good practice to execute Apps in isloation each using its own explicit dependencies. It is also recommended to keep App secrets and config data outside the App so as to allow easy switch from dev server to production.

In this tutorial I am explaining :

What are Virtual environments? How to use it ?
What is Environment Variables? How can we separate configuration from code.

What is Virtual Environment ?

Imagine you have two DRF Apps. The first one is a legacy App using old versions of DRF and Python. The second one is using the latest version of Python and Drf. Obviously if you try to place them together in the same environment , it may lead to conflicts. What we want is that each App should use its own version of Python, DRF and so on; independent of other Apps.

Virtual environment allows you to have multiple versions of DRF, Python and other dependencies on your machine without any conflicts. Thus it isolates us from what is installed at the host machine. Instead of using/installing softwares at OS level, we can place them inside the /bin folder of the virtual environment.

Virtual Environment Step By Step

Install

pip install virtualenv

Create a new Environment

virtualenv BestApiAppEver

Activate

chmod 755 BestApiAppEver/bin/activate
BestApiAppEver/bin/activate
source BestApiAppEver/bin/activate

Install dependencies

cd BestApiAppEver
pip3 install django
pip3 install djangorestframework
pip3 install django-environ

Create project followed by App

django-admin startproject BestApiAppEver .
python3 manage.py startapp helloapp

Freeze:

Freezing allows us to save packages that were installed using pip in the virtual environment. This creates a file which may be later used to install exactly same dependencies else where. The command below will create a file requirements.txt and stores dependencies in that file.

pip3 freeze > requirements.txt

This is created requirements.txt file :

If we want to install same dependencies else-where, we may use

pip3 install -r requirements.txt

Environment Variables

When an app is moved to a different host or deployment option, code and dependencies remains the same but configuration changes. It is therefore required to keep config separate from code.. In order to manage App secrets, configuration data (database settings, caching, external API keys etc) and allow seamless transition among staging, production and development environments etc it is recommended to keep config data separate from Application.

What is django-environ ?

It is a Python package that allows us to configure your App with environment variables.

How to Install

Following instructions as given in this page.

How to use?

Create a .env file project root and store one or more variables in the form KEY = value as follows:

Now modify settings.py in project root as follows:

Now we can read MESSAGE variable from settings.py any where in App as follows:

Remember:
Virtual Environments are created to manage App dependencies independent of OS and other Apps.
Environment Variables allows us to separate code from configuration data such as connection string, private keys, API URLs and so on.

You may download sample code from this repository.

Happy Coding.

--

--