Monday, February 16, 2026

Python: virtual environments (venv) and environment variables (.env)

 

The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site ...
In Python, creating a "private environment" generally refers to two primary concepts:
 
- virtual environments for project isolation and managing dependencies, and 
- using environment variables and .env files for securely managing sensitive information (secrets).
1. Project Isolation with Virtual Environments (venv)
 
A virtual environment is a self-contained directory that contains a specific Python interpreter and its own isolated set of Python packages. This prevents conflicts between the dependencies of different projects and keeps your global Python installation clean.
Key Steps:
  • Creation: Navigate to your project directory in the terminal and run the following command to create a virtual environment named .venv (a common convention):
    bash
    python -m venv .venv
    
    This creates a directory containing the isolated Python installation.
  • Activation: Before working on your project, you must activate the environment:
    • macOS/Linux:
      bash
      source .venv/bin/activate
      
    • Windows:
      bash
      .venv\Scripts\activate
      
    Once activated, your terminal prompt will usually change to indicate the active environment (e.g., (.venv) YourName: ).
  • Deactivation: To leave the virtual environment, simply run:
    bash
    deactivate
    
  • Version Control: You should add the environment directory (e.g., .venv) to your .gitignore file to prevent accidentally committing it to source control. The environment is considered disposable and can be recreated from a requirements.txt file at any time.
  • Managing Python Versions: For managing multiple Python versions themselves on a single system (e.g., switching between Python 3.10 and 3.12), tools like pyenv can be used alongside venv.
 
2. Storing Secrets with .env Files
 
To keep sensitive information like API keys, passwords, and credentials out of your source code, you use environment variables loaded from a .env file.
Key Steps:
  • Create a .env file: In your project's root directory, create a file named .env. Inside this file, store key-value pairs (e.g., API_KEY=your_secret_key).
    • Crucial: Add the .env file to your .gitignore file immediately to prevent it from being committed to source control.
  • Install python-dotenv: Install the required package using pip within your activated virtual environment:
    bash
    pip install python-dotenv
    
    You can find the package on the official PyPI repository.
  • Load Variables in Python: In your Python code (e.g., app.py), use the load_dotenv function to read the variables, and access them using the standard os module.
    python
    import os
    from dotenv import load_dotenv
    
    # Load environment variables from .env file
    load_dotenv()
    
    # Access the variables
    api_key = os.getenv("API_KEY")
    database_url = os.getenv("DATABASE_URL")
    
    print(f"Key: {api_key}")
    
This ensures your sensitive data is kept private and can be easily managed across different deployment environments.

No comments:

Blog Archive

Followers