杰瑞科技汇

Python环境变量如何正确配置与管理?

Of course! Understanding environment variables is a fundamental skill in Python development. They are a crucial part of building secure, configurable, and portable applications.

Python环境变量如何正确配置与管理?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering what they are, why they're important, and how to use them in Python.


What are Environment Variables?

Environment variables are key-value pairs stored outside of your code. They are part of the environment in which a program runs.

Think of them as a global configuration layer for your operating system and the applications running on it.

  • Key: The name of the variable (e.g., API_KEY, DATABASE_URL).
  • Value: The data stored in the variable (e.g., sk-123..., postgresql://user:pass@host/db).

A key feature is that they are not hard-coded in your source files. This separation is what makes them so powerful.

Python环境变量如何正确配置与管理?-图2
(图片来源网络,侵删)

Why Use Environment Variables?

Using environment variables is a best practice for several reasons:

  1. Security: Never hard-code sensitive information like API keys, database passwords, or secret tokens directly in your code. If you commit your code to a public repository (like GitHub), those secrets will be exposed. Environment variables keep them separate and secure.
  2. Configuration Management: Your application might need to behave differently in different environments (development, testing, production).
    • Development: DEBUG=True, DB_URL=sqlite:///dev.db
    • Production: DEBUG=False, DB_URL=postgresql://user:prod@prod-db/prod_db You can easily switch configurations by changing the environment variables without touching your code.
  3. Portability: Environment variables are a standard way to configure applications across different operating systems (Windows, macOS, Linux) and deployment platforms (Docker, servers, serverless functions).
  4. Separation of Concerns: Your code focuses on what to do, while the environment specifies how to run it. This keeps your code clean and decoupled from its specific runtime context.

How to Set Environment Variables

Before you can use them in Python, you need to set them.

On macOS / Linux

Use the export command in your terminal. This sets the variable for the current session only.

# Set a single variable
export API_KEY="your_secret_api_key_here"
# Set another variable
export DATABASE_URL="postgresql://user:password@localhost/mydb"
# View the variable
echo $API_KEY
# Output: your_secret_api_key_here

To make them permanent, add the export commands to your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc).

Python环境变量如何正确配置与管理?-图3
(图片来源网络,侵删)

On Windows (Command Prompt)

Use the set command.

C:\> set API_KEY="your_secret_api_key_here"
C:\> set DATABASE_URL="postgresql://user:password@localhost/mydb"
# View the variable
C:\> echo %API_KEY%
# Output: "your_secret_api_key_here"

On Windows (PowerShell)

Use the $env: prefix.

PS C:\> $env:API_KEY="your_secret_api_key_here"
PS C:\> $env:DATABASE_URL="postgresql://user:password@localhost/mydb"
# View the variable
PS C:\> echo $env:API_KEY
# Output: your_secret_api_key_here

How to Access Environment Variables in Python

The standard library module for this is os.environ.

Basic Access

You can access an environment variable like a dictionary.

import os
# Access an existing variable
api_key = os.environ.get('API_KEY')
database_url = os.environ.get('DATABASE_URL')
print(f"API Key: {api_key}")
print(f"Database URL: {database_url}")
# If the variable doesn't exist, os.environ.get() returns None by default.
# This is safe and prevents your program from crashing.
debug_mode = os.environ.get('DEBUG_MODE') # If not set, debug_mode will be None
print(f"Debug Mode: {debug_mode}")

Handling Missing Variables (The Right Way)

Using .get() is good, but for critical variables that your application must have, you should use .pop() with a default value. This will raise a clear error if the variable is missing, which is much better than a cryptic error later in your program.

import os
# Use .pop() for required variables.
# If 'API_KEY' is not set, this will raise a KeyError with a helpful message.
try:
    api_key = os.environ.pop('API_KEY')
    print(f"Successfully retrieved API Key.")
except KeyError:
    print("ERROR: The API_KEY environment variable is not set. Exiting.")
    # You might want to sys.exit(1) here

Providing a Default Value

You can provide a default value if the environment variable is not set.

import os
# If 'DEBUG_MODE' is not set, it will default to 'False'
debug_mode = os.environ.get('DEBUG_MODE', 'False')
# If 'PORT' is not set, it will default to 8000
# Note: The value from the environment is always a string, so you must cast it.
port = int(os.environ.get('PORT', 8000))
print(f"Debug Mode is: {debug_mode}")
print(f"Running on port: {port}")

Best Practice: Using a .env File

Manually setting environment variables every time you open a terminal is tedious. The standard solution is to use a .env file in your project's root directory. This file will store your variables locally.

Step 1: Install the python-dotenv library

pip install python-dotenv

Step 2: Create a .env file

In your project's root directory, create a file named .env. Important: Add .env to your .gitignore file to avoid committing secrets!

# .env file
# API Configuration
API_KEY=sk-1234567890abcdef
API_URL=https://api.example.com/v1
# Database Configuration
DATABASE_URL=postgresql://user:mysecretpassword@localhost:5432/mydatabase
# App Configuration
DEBUG=True
PORT=8000

Step 3: Load variables in your Python code

Modify your Python script to load the variables from the .env file at the beginning.

import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Now you can access the variables as before
# The values from the .env file will be available
api_key = os.environ.get('API_KEY')
database_url = os.environ.get('DATABASE_URL')
debug_mode = os.environ.get('DEBUG', 'False') # Get from .env or default
print(f"API Key from .env: {api_key}")
print(f"Database URL from .env: {database_url}")
print(f"Debug Mode from .env: {debug_mode}")
# Your application code continues here...

Why is load_dotenv() so useful?

  • Development: Developers can have their own local .env files with their personal keys or settings.
  • Production: Your deployment scripts can set the real environment variables on the server, and your code will read them from the system (overriding any values in a local .env file). This gives you precedence: System Environment > .env file.

Summary and Key Takeaways

Task Method Example
Set in Terminal (macOS/Linux) export VAR_NAME="value" export FLASK_ENV="development"
Set in Terminal (Windows CMD) set VAR_NAME="value" set FLASK_ENV="development"
Set in Terminal (Windows PS) $env:VAR_NAME="value" $env:FLASK_ENV="development"
Access in Python import os
value = os.environ.get('VAR_NAME')
db_url = os.environ.get('DATABASE_URL')
Access with Default value = os.environ.get('VAR_NAME', 'default_value') port = int(os.environ.get('PORT', 5000))
Access Required Variable value = os.environ.pop('VAR_NAME') (raises KeyError if missing) api_key = os.environ.pop('API_KEY')
Best Practice (Local Dev) Use a .env file with the python-dotenv library. pip install python-dotenv
load_dotenv()
value = os.environ.get(...)
分享:
扫描分享到社交APP
上一篇
下一篇