DEV Community

Shantanu Nighot
Shantanu Nighot

Posted on • Originally published at magbanum.com on

How to save Environment Variables in Python virtual env

Saving environment variables in a Python virtual environment can be a great way to keep your project's dependencies and settings organized and separate from other tasks on your machine.

I use Django to create APIs for the front end of my website. Django needs many credentials that should not be exposed to the public or in the GitHub repository. I have already created an article on How to hide Django SECRET_KEY on Public Repositories. But if we want to use it on a local machine then still have to replace the code with the original credentials.

Here are the steps to take to save environment variables in a Python virtual environment:

Create a virtual environment:

To create a virtual environment, you can use the virtualenv package. To install it, open a terminal and run pip install virtualenv. Once it is installed, you can create a new virtual environment by running a command virtualenv venv, where "venv" is the name of your virtual environment.

Activate the virtual environment:

To activate the virtual environment, run source myenv/bin/activate on Linux and macOS or source myenv\Scripts\activate on Windows. Once the virtual environment is active, the name of the virtual environment will appear in the command prompt.

Set environment variables:

To set an environment variable, you can use the export command on Linux and macOS or the set command on Windows. For example, to set an environment variable named DJANGO_SETTINGS_MODULE with the value myproject.settings, you can run export DJANGO_SETTINGS_MODULE=myproject.settings on Linux and macOS or set DJANGO_SETTINGS_MODULE=myproject.settings on Windows.

Keep in mind that the environment variables set through the command prompt are temporarily saved in the environment and will be destroyed once the environment is deactivated. Use the following method to set the variables automatically every time the environment is activated.

Save environment variables:

To save environment variables so they persist even after you close the terminal or deactivate the virtual environment, you need to add the export or set commands to a shell script.

On Linux and macOS, create a file named activate in the bin directory of your virtual environment with the following contents:

#!/bin/sh
export SECRET_KEY="django-insecure- *************"

Enter fullscreen mode Exit fullscreen mode

On Windows, find the file named activate or create a file named activate in the Scripts directory of your virtual environment with the following contents:

set SECRET_KEY="django-insecure- *************"

Enter fullscreen mode Exit fullscreen mode

Use saved environment variables:

We can use the saved variables using os.environ.get(). In my case, I want to use the saved secret key for my Django project. In settings.py file, I will import the OS module using import os. Then I will get the saved environment variable SECRET_KEY using the command os.environ.get('SECRET_KEY').

Note: Make sure you do not expose activate file in your GitHub repository. To ensure this add your env directory to .gitignore file. Directories and files included in .gitignore are not included while pushing the code to the repository.

Deactivate the virtual environment:

To deactivate the virtual environment, you can run the deactivate command. This will take you out of the virtual environment and return to the global Python installation on your machine.

By following these steps, you can successfully save environment variables in a Python virtual environment, keeping your project dependencies and settings organized and separate from other projects on your machine.

Please let me know if I am missing something in this tutorial or need help with any step.

Thanks for reading. See you in the next article.

Top comments (0)