Python Environment Variables

Environment variables in Python control how the Python interpreter behaves and where it searches for modules and configuration files. Understanding these variables helps you customize Python's runtime environment and resolve import issues.

Key Python Environment Variables

Variable Description Example Usage
PYTHONPATH Specifies additional directories where Python searches for modules. Similar to the system PATH variable but for Python imports. export PYTHONPATH=/path/to/modules
PYTHONSTARTUP Path to a Python file that runs automatically when the interactive interpreter starts. Useful for loading common imports or utilities. export PYTHONSTARTUP=~/.pythonrc.py
PYTHONCASEOK On Windows, enables case-insensitive module imports. Set to any value to activate. set PYTHONCASEOK=1
PYTHONHOME Sets the location of the standard Python libraries. Useful when Python is installed in a non-standard location. export PYTHONHOME=/usr/local/python

Working with PYTHONPATH

The most commonly used environment variable is PYTHONPATH. Here's how to check and modify it ?

import sys
import os

# Check current Python path
print("Current PYTHONPATH:")
for path in sys.path:
    print(f"  {path}")

# Check PYTHONPATH environment variable
pythonpath = os.environ.get('PYTHONPATH')
print(f"\nPYTHONPATH environment variable: {pythonpath}")
Current PYTHONPATH:
  /usr/lib/python39.zip
  /usr/lib/python3.9
  /usr/lib/python3.9/lib-dynload
  /usr/local/lib/python3.9/dist-packages

PYTHONPATH environment variable: None

Setting Environment Variables

Linux/macOS

# Temporary (current session only)
export PYTHONPATH="/path/to/your/modules:$PYTHONPATH"

# Permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export PYTHONPATH="/path/to/your/modules:$PYTHONPATH"' >> ~/.bashrc

Windows

# Temporary (current session only)
set PYTHONPATH=C:\path\to\your\modules;%PYTHONPATH%

# Permanent (use System Properties > Environment Variables)
setx PYTHONPATH "C:\path\to\your\modules;%PYTHONPATH%"

Practical Example

Create a startup file to automatically import common modules ?

# Create ~/.pythonrc.py
import os
import sys
import datetime

print(f"Python {sys.version}")
print(f"Started at {datetime.datetime.now()}")
print("Common modules imported: os, sys, datetime")

# Add custom module path
sys.path.append('/home/user/my_modules')

Then set the PYTHONSTARTUP variable ?

export PYTHONSTARTUP="$HOME/.pythonrc.py"

Checking Environment Variables in Python

import os

# Check all Python-related environment variables
python_vars = {key: value for key, value in os.environ.items() 
               if key.startswith('PYTHON')}

print("Python Environment Variables:")
for var, value in python_vars.items():
    print(f"{var}: {value}")

if not python_vars:
    print("No Python environment variables set")
Python Environment Variables:
No Python environment variables set

Conclusion

Python environment variables like PYTHONPATH and PYTHONSTARTUP provide powerful ways to customize your Python environment. Use PYTHONPATH to add module search directories and PYTHONSTARTUP to automatically load common imports in interactive sessions.

Updated on: 2026-03-25T07:27:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements