A better way to manage your environment variables is to store them in a
.env (pronounced dot env) file.
.env file is a text file in which the variables are defined, one per line. The format of a
.env file is exactly the same under all operating systems, so
.env files make working with environment variables uniform across all platforms. And as if this isn’t enough, having your environment variables written in a file that is automatically imported by Python means that you don’t have to manually set them every time you start a new shell.
You can create a
.env file in the root directory of each of your projects, and that way you can keep all the variables that are needed by each project neatly organized!
The python-dotenv package allows a Python application to import variables defined in a
.env file into the environment. You can install
python-dotenv in your virtual environment using pip:
dotenv import load_dotenv
To set the value to the environment user variable:
os.environ['USER'] = 'Bob'
If you need to clear a single environment variable in the session you can use
os.environ.pop() with the key and if you need to clear all environment variables you can use
environment variables can be called like so:
From dotenv import load_dotenv
token = os.environ.get("USER")
Hope this will help you.