python
environment variables
programming
tutorial
os module

How can I access environment variables in Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Accessing environment variables in Python is a common need, particularly in applications that require configuration information that may vary by deployment environment. Python provides straightforward ways to interact with these environment variables using its standard library. Below, we explore various techniques and best practices for accessing and utilizing environment variables in Python applications.

Environment Variables in Python

Environment variables are key-value pairs that are accessible globally across running processes on an operating system. They serve multiple purposes, including holding configuration, system paths, and other sensitive information required by applications. In Python, these can be accessed using the os module.

Using the os Module

The os module in Python offers various methods to deal with environment variables.

  • Accessing Variables: You can access environment variables using os.environ, which acts like a dictionary where each key-value pair is an environment variable.
python
1  import os
2
3  # Accessing an existing environment variable
4  path = os.environ.get('PATH')
5  print(f"PATH: {path}")
6
7  # Accessing an environment variable with a default value
8  home_dir = os.environ.get('HOME', '/default/home')
9  print(f"Home Directory: {home_dir}")
10
11  # Accessing an environment variable directly
12  try:
13      secret_key = os.environ['SECRET_KEY']
14      print(f"Secret Key: {secret_key}")
15  except KeyError:
16      print("SECRET_KEY is not set!")
  • Setting Variables: You can also set environment variables using os.environ or os.putenv.
python
1  # Setting an environment variable
2  os.environ['NEW_VARIABLE'] = '12345'
3  print(os.environ['NEW_VARIABLE'])
4  
5  # Using os.putenv (Note: os.putenv may not update os.environ, so direct assignment is recommended)
6  os.putenv('ANOTHER_VARIABLE', 'abcde')
  • Removing Variables: While uncommon, it is possible to remove environment variables by using del on os.environ.
python
1  # Removing an environment variable
2  if 'NEW_VARIABLE' in os.environ:
3      del os.environ['NEW_VARIABLE']
4      print("NEW_VARIABLE removed")

Best Practices

  • Use Environment Variables for Sensitive Information: Store passwords, API keys, or other sensitive information in environment variables instead of hardcoding them in your source code.
  • Check for Key Existence: Always check whether an environment variable exists and have a fallback/default value.
  • Environment Configuration Files: Tools like python-dotenv can help manage environment variables using a .env file, which is especially useful during development.
python
1  # Using python-dotenv to load environment variables
2  from dotenv import load_dotenv
3  load_dotenv()  # Automatically loads variables from a .env file
4
5  database_url = os.getenv('DATABASE_URL', 'sqlite:///:memory:')

Common Use Cases

  • Configuration Management: Store different configurations like database URLs, API endpoints, etc., which may change based on the environment (development, testing, production).
  • Secrets Management: Manage sensitive information securely without hardcoding in code repositories.
  • Dynamic Execution: Modify application behavior dynamically without changing the code by tweaking environment variables.

Key Points Summary

Below is a table summarizing the key aspects of handling environment variables in Python:

AspectDescription
Accessing VariablesUse os.environ.get('VAR') for safe access with a default value. Use os.environ['VAR'] for mandatory variables.
Setting VariablesDirectly use os.environ['VAR'] = 'value' or os.putenv().
Removing VariablesUse del os.environ['VAR'] to remove a variable.
Best PracticesFavor os.environ.get() with defaults. Use environment files in development. Store sensitive data in variables.
Common Librariespython-dotenv for managing .env files.

Understanding how to effectively use environment variables can significantly enhance the security, flexibility, and maintainability of your Python applications. By following the approaches discussed, including making use of the os module and libraries like python-dotenv, you can streamline configuration management across different deployment environments.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.