Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Environment variable in Python

In Python, environment variables are a way to store system-wide settings or user-specific configurations that can be accessed by applications. You can access and manipulate environment variables in Python using the os module.

  • Importing the os module:

Before you can access environment variables, you need to import the os module.

import os
  • Accessing environment variables:

Use the os.environ dictionary to access environment variables. The keys of this dictionary are the names of the environment variables.

Example:

import os

# Get the value of the 'PATH' environment variable
path = os.environ['PATH']
print(path)

# Alternatively, you can use the .get() method to avoid KeyError if the variable doesn't exist
path = os.environ.get('PATH', 'Default Value')
print(path)
  • Setting environment variables:

You can set the value of an environment variable by assigning a value to a key in the os.environ dictionary. Note that this change will only affect the current process and its child processes; it will not change the environment variable system-wide.

Example:

import os

# Set the value of the 'MY_VARIABLE' environment variable
os.environ['MY_VARIABLE'] = 'My Value'

# Verify that the variable is set
my_variable = os.environ.get('MY_VARIABLE')
print(my_variable)  # Output: My Value
  • Deleting environment variables:

To delete an environment variable, you can use the del keyword. Note that this will only remove the variable from the current process and its child processes, not system-wide.

Example:

import os

# Delete the 'MY_VARIABLE' environment variable
if 'MY_VARIABLE' in os.environ:
    del os.environ['MY_VARIABLE']

# Verify that the variable is deleted
my_variable = os.environ.get('MY_VARIABLE')
print(my_variable)  # Output: None

In summary, you can use the os module in Python to access and manipulate environment variables. Use the os.environ dictionary to access, set, or delete environment variables. Note that changes made to environment variables in Python will only affect the current process and its child processes, not system-wide settings.

  1. Setting environment variables in Python:

    • Description: Environment variables are set using the os.environ dictionary.
    • Example Code:
      import os
      
      os.environ['MY_VARIABLE'] = 'my_value'
      
  2. Accessing environment variables in Python:

    • Description: Access environment variables using the os.environ dictionary.
    • Example Code:
      import os
      
      my_variable = os.environ.get('MY_VARIABLE', 'default_value')
      
  3. Using os module for environment variables in Python:

    • Description: The os module provides functions for working with environment variables.
    • Example Code:
      import os
      
      os.putenv('MY_VARIABLE', 'my_value')
      
  4. Environment variable manipulation in Python:

    • Description: Modify or delete environment variables using the os.environ dictionary.
    • Example Code:
      import os
      
      os.environ['MY_VARIABLE'] = 'new_value'
      del os.environ['MY_VARIABLE']
      
  5. Loading environment variables from a file in Python:

    • Description: Load environment variables from a file using the python-dotenv library.
    • Example Code:
      from dotenv import load_dotenv
      
      load_dotenv('.env')
      
  6. Security considerations for Python environment variables:

    • Description: Avoid hardcoding sensitive information directly in code. Use secure methods like key vaults for sensitive data.
    • Example Code:
      import os
      
      API_KEY = os.environ.get('API_KEY')  # Avoid hardcoding sensitive information
      
  7. Passing environment variables to subprocesses in Python:

    • Description: Use the subprocess module to pass environment variables to subprocesses.
    • Example Code:
      import subprocess
      import os
      
      subprocess.run(['echo', os.environ['MY_VARIABLE']])
      
  8. Environment variable persistence in Python scripts:

    • Description: Environment variables set in a script are only available for the duration of that script. They don't persist after the script finishes.
    • Example Code:
      import os
      
      os.environ['TEMP_VAR'] = 'temporary_value'
      
      # The TEMP_VAR environment variable is only available within this script