How to Set and Get Environment Variables in Python

Ask questions Research chat →

https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5 · scraped

python

Attachments

Scraped Content

— 344 words · 2026-02-14 17:43:40 UTC ·

Excerpt

To set and get environment variables in Python you can just use the os module: ```plain text import os # Set environment variables os.environ['API_USER'] = 'username' os.environ['API_PASSWORD'] = 'secret' # Get environment variables USER = os.getenv('API_USER') PASSWORD = os.environ.get('API_PASSWORD') # Getting non-existent keys FOO = os.getenv('FOO') # None BAR = os.environ.get('BAR') # None BAZ = os.environ['BAZ'] # KeyError: key does not exist. ``` Note that using getenv() or the get() method on a dictionary key will return None if the key does not exist. However, in the example with BAZ, if you reference a key in a dictionary that does not exist it will raise a KeyError. Environment variables are useful when you want to avoid hard-coding access credentials or other variables into code. For example, you may need to pass in API credentials for an email service provider in order to send email notifications but you wouldn’t want these credentials stored in your code repository.
To set and get environment variables in Python you can just use the os module: ```plain text import os # Set environment variables os.environ['API_USER'] = 'username' os.environ['API_PASSWORD'] = 'secret' # Get environment variables USER = os.getenv('API_USER') PASSWORD = os.environ.get('API_PASSWORD') # Getting non-existent keys FOO = os.getenv('FOO') # None BAR = os.environ.get('BAR') # None BAZ = os.environ['BAZ'] # KeyError: key does not exist. ``` Note that using getenv() or the get() method on a dictionary key will return None if the key does not exist. However, in the example with BAZ, if you reference a key in a dictionary that does not exist it will raise a KeyError. Environment variables are useful when you want to avoid hard-coding access credentials or other variables into code. For example, you may need to pass in API credentials for an email service provider in order to send email notifications but you wouldn’t want these credentials stored in your code repository. Or perhaps you need your code to function slightly differently between your development, staging and production environments. In this case you could pass in an environment variable to tell your application what environment it’s running in. These are typical use cases for environment variables. ## Storing local env variables You should write your Python code so that it is able to access environment variables from whatever environment it is running in. This could be either your local virtual environment that you’re using for development or a service that you are hosting it on. A useful package that simplifies this process is Python Decouple, this is how you would use it. First install Python Decouple into your local Python environment. ```plain text $ pip install python-decouple ``` Once installed, create a .env file in the root of your project which you can then open up to add your environment variables. ```plain text $ touch .env # create a new .env file $ nano .env # open the .env file in the nano text editor ``` ‌You can then add your environment variables like this: ```plain text USER=alex KEY=hfy92kadHgkk29fahjsu3j922v9sjwaucahf ```

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation