- When to Use Python Environment Variables
- Set Environment Variables in Python
- Store Local Environment Variables in Python
In this article, we will discuss the environment variables in Python. We will find out how these variables work and why we need these variables.
When to Use Python Environment Variables
We use environment variables in our Python scripts for greater security and smoother operations. First, let’s decide when it’s alright to utilize them.
Remember that environment variables don’t seem to be associated with the program core by definition. This means that it’s ideal to utilize it when the variable has to change with the environment to stay programmed up to this point.
Identification keys (like the API token example given) and operation mode are two common use cases for environment variables (e.g., development, staging, production).
Set Environment Variables in Python
Let’s now observe a way to put these variables into action. We’ll ultimately include them into our code, except for now, we’ll flame up our Python interpreter and understand the fundamentals together.
In Python programming, to use environment variables, the foremost critical step is to import the operating system into the software we’re using; without it, the program won’t run, and no tasks will be completed.
# python import os
We will get
None if we utilize the
get() function on a dictionary key that does not exist in the
Referencing a key in a dictionary that does not exist in
FLY ends in a
keyerror, as shown below.
# python import os # Setting environment variables os.environ["block_user"] = 'username' os.environ['block_Pass'] = 'private' # Getting environment variables User = os.getenv('block_user') Pass = os.environ.get('block_pass') # Getting non-existent keys superAdmin = os.getenv('superadmin') # Returns None Admin = os.environ.get('admin') # Returns None FLY = os.environ['FLY'] # KeyError: key does not exist.
Store Local Environment Variables in Python
Python code should be written to access environment variables from whatever environment it is executing. This might be our local virtual environment for development or a service on which we’re hosting it.
Decouple is a great library that creates this procedure easier. It must first be installed in our local Python environment using the below command.
# python pip install python-decouple
After installing, make a
.env file in the root of our program and edit it to add our environment variables, as shown below.
# python $ touch .env $ nano .env
As you can see, we used
touch .env to create a new
.env file and the
nano command to open the
.env file in the nano text editor.
After that, we will add our environment variables as follows:
# .env USER = superman PASS = hfy77manHgkk
Then, we will save the file and exit nano. Our .env file now contains our environment variables.
When using Git, we should add
.env to our
.gitignore file to prevent this secret file from being submitted to the code repository.
Once we’ve saved our environment variables in a
.env file, we can use them in your Python code, as shown below.
# python from decouple import config block_USERNAME = config('USER') block_KEY = config('PASS')
The advantage of utilizing a technique like the one described above is that you can establish your environment variables using whatever method or interface the cloud service provider provides. Our Python code should still be able to access them.
It’s worth noting that using capital letters for the names of global constants in our code is standard practice. The environment variables for staging or production environments may usually be configured via a CLI or web interface most cloud service providers provide.
We’ll need to consult their documentation on establishing environment variables while utilizing their service for help in these circumstances.