In this Python Django tutorial, I will explain what are environment variables in Django and how to print django environment variables.
Django environment variables
The environment variables are variables that store sensitive information related to the project or applications.
Modern web projects or applications are required to access sensitive information such as API keys, Database credentials, etc. And this kind of information should not be exposed to the public.
But “how environment variables are created?”, to create an environment variable, you simply create a variable and assign a value to it. In general, environment variables are made of key-value pairs.
Where key is the variable name and value is the data that is assigned to that variable. Let me show you the syntax:
VARIABLE_NAME = value
Here, VARIABLE_NAME is the name of the environment variable and the value is the exact data that is associated with the variable name.
Now that you know how to create environment variables, environment variables in Django are created in a specific file called .env (read as dot env).
Create Environment Variables in Django
As you know, environment variables in Django are defined in a special file called .env. So open your Django project and create a new file named .env at the root level.
For example, think that the below given is a newly created project in Django. So, at the root directory of the project, create a new file named .env as shown below.
In the above output, you can see that the file .env is created at the root directory of your project. If you are not sure where to create this file, simply remember that wherever you see the manage.py file at that level create a .env file.
Now you learned how to create a file that stores the environment variables in Django. Let’s move and see how to define the environment variables in Django.
Define Environment Variables in Django
To define environment variables in Django, open the file .env and for example, add the below given environment variables in that file.
# The URL for connecting to MySQL database with username, password and database
# API keys to access the resources somewhere on the internet
In the above picture, you can see that the .env file contains two environment variables in Django.
The first environment variable is DATABASE_URL with the value ‘mysql://root:12345@localhost/envDatabase’ and the second is API_KEY with the value ‘sknafs34440afdlnsldf’.
This is how you define the environment variables in a real-world project, but how you are going to print or access the environment variables throughout your Django project. The next section is about accessing or printing the environment variables in Django.
Print Django Environment variables
In Django, you can print or access the environment variables that are defined in the .env file.
Here, I will show how to access or print the environment variables in Django using the package python-dotenv.
Printing Environment Variables in Django Using Dotenv
Open the Python shell and run the following command to install the package python-dotenv.
pip install python-dotenv
After installing, open the Python shell using the below command.
python manage.py shell
In the Python shell, write the following command to print the environment variables defined in the .env file.
from dotenv import load_dotenv
database_url = os.getenv('DATABASE_URL')
api_key = os.getenv('API_KEY')
In the above picture, the code ‘from dotenv import load_dotenv’ imports the load_dotenv function from the module ‘dotenv’.
- After that, import the Python standard library called os, which allows it to interact with the operating system and environment variables.
- Then, the function load_dotenv() is called to read the environment variables from the file .env and load this variable into the current shell.
- After this, access the environment variable value using the function os.getenv(), and this function accepts the environment variable names that are defined in the .env file. So here, access two environment variables DATABASE_URL and API_KEY.
- Finally printing the environment variables in the Python shell using the print() function.
As you can see in the output, the values of both environment variables are printed into the shell.
In this Python Django tutorial, you learned about environment variables in Django, then you created a special file called .env to store the environment variables. After that, you learned how to access the defined environment variables in that file using the package python-dotenv.
You may also like to read:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.