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
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.
os
module:Before you can access environment variables, you need to import the os
module.
import os
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)
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
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.
Setting environment variables in Python:
os.environ
dictionary.import os os.environ['MY_VARIABLE'] = 'my_value'
Accessing environment variables in Python:
os.environ
dictionary.import os my_variable = os.environ.get('MY_VARIABLE', 'default_value')
Using os
module for environment variables in Python:
os
module provides functions for working with environment variables.import os os.putenv('MY_VARIABLE', 'my_value')
Environment variable manipulation in Python:
os.environ
dictionary.import os os.environ['MY_VARIABLE'] = 'new_value' del os.environ['MY_VARIABLE']
Loading environment variables from a file in Python:
python-dotenv
library.from dotenv import load_dotenv load_dotenv('.env')
Security considerations for Python environment variables:
import os API_KEY = os.environ.get('API_KEY') # Avoid hardcoding sensitive information
Passing environment variables to subprocesses in Python:
subprocess
module to pass environment variables to subprocesses.import subprocess import os subprocess.run(['echo', os.environ['MY_VARIABLE']])
Environment variable persistence in Python scripts:
import os os.environ['TEMP_VAR'] = 'temporary_value' # The TEMP_VAR environment variable is only available within this script