Python 3 Django Environment Variable Module Tutorial to Setup and Load Variables From env File Format

Thanks for reading my article first. Python 3 Django Environment Variable Module Tutorial to Setup and Load Variables From env File Format We are going to learn more about this in this article. Let’s go into the article

When hosting a Python WSGI compatible framework such as Django on Apache with mod_wsgi, only context variables in the os.environ dictionary are in the context of the script starting in Apache. But instead of messing with Apache’s service manager settings (eg systemd or systemctl), there is a better way.

Python 3 Django Environment Variable Module Tutorial to Setup and Load Variables From env File Format

Python 3 Django Environment Variable Module Tutorial to Setup and Load Variables From env File Format

Django-Envie

This module helps to set environment variable for your Django application from a .env.py or .env.yml file placed inside the project directory.

Installation

pip install django-envie

Usage

To use, create an environment file, save as either a .env.py or .env.yml file format in your django project (inside the project directory) or repository root (outside the project directory).

Configuration

The .env.py file for a django project should be formatted this way:

DB_NAME = "sector_seven"
DB_USER = "homer_simpson"
DB_PASSWORD = "Close, but you're way off."

If using .env.yml file format

DB_NAME: "sector_seven"
DB_USER: "homer_simpson"
DB_PASSWORD: "Close, but you're way off."

In the project settings script, include this snippet:

from django_envie import load_vars

load_vars()

Accessing your environment variables

Accessing environment variables anywhere can be done by using

os.getenv('DB_NAME')

Setting up your environment variable for Continuous Integration

CircleCI

KEY = VALUE

TravisCI

KEY = "VALUE"

Check out the documentation for this project here

Read Also: Python 3 Django Tutorial to Build News Website in Browser

Conclusion

Python 3 Django Environment Variable Module Tutorial to Setup and Load Variables From env File Format We hope this article solves all your doubts. If in doubt let me know.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment