Python - Environment
Key Environment Variables
PYTHONUNBUFFERED=1
Forces stdout and stderr streams to be unbuffered. Highly recommended in Docker containers and CI/CD environments so application logs flush immediately without getting stuck in process buffers.
# In Dockerfile or shell
ENV PYTHONUNBUFFERED=1
PYTHONDONTWRITEBYTECODE=1
Prevents Python from writing .pyc bytecode files into __pycache__/ directories. Commonly used in production containers to keep the filesystem immutable and clean.
export PYTHONDONTWRITEBYTECODE=1
PYTHONPATH
Augments the default search path for module files. Directories specified in PYTHONPATH are inserted before the standard library search locations in sys.path.
export PYTHONPATH="${PYTHONPATH}:/path/to/custom/libs"
PYTHONHASHSEED
Controls the seed for Python's cryptographic hash randomization (enabled by default for str and bytes to prevent denial-of-service hash-collision attacks). Setting PYTHONHASHSEED=0 disables randomization to produce deterministic hash values during reproducible testing.
export PYTHONHASHSEED=0
VIRTUAL_ENV
Set automatically when an active virtual environment is activated via source .venv/bin/activate. Points to the root directory of the virtual environment.
Inspecting Environment at Runtime
import os
import sys
# View all active environment variables
api_key = os.environ.get("API_KEY", "default_fallback")
# View module search path
print(sys.path)
# Verify whether running inside a virtual environment
is_venv = sys.prefix != sys.base_prefix

