Python site Customization — ELI5
When you turn on a computer, it does not jump straight to your apps. First, it loads the operating system, sets up the screen, connects to Wi-Fi, and gets everything ready. Only then does it show you the desktop.
Python does something similar. Before your code runs, a module called site wakes up and prepares the environment. It does three important things, all behind the scenes.
First, it figures out where your installed packages live. When you run pip install requests, the requests library goes into a specific folder. The site module finds that folder and adds it to Python’s search path so that import requests works. Without this step, Python would not know where to look for any third-party library.
Second, it checks for customization files. There are two special files — sitecustomize.py and usercustomize.py — that Python runs automatically at startup. Companies use sitecustomize.py to configure logging, set default encodings, or enable monitoring tools for every Python program on a server. Individual developers use usercustomize.py for personal tweaks.
Third, it sets up a few helpful built-in things like the quit() and exit() commands in the interactive shell. Without the site module, typing quit() in the Python prompt would give you an error.
You can see what site configured by running python -m site in your terminal. It shows you all the paths Python searches and where your packages folder is.
Most of the time this all happens invisibly. But when an import mysteriously fails or packages seem to install in the wrong place, understanding site is the key to debugging it.
The one thing to remember: The site module is Python’s invisible setup step — it configures package paths, runs startup customization files, and prepares the environment before a single line of your code executes.
See Also
- Python Ast Module Code Analysis How Python's ast module reads your code like a grammar teacher diagrams sentences — turning source text into a tree you can inspect and change.
- Python Dis Module Bytecode How Python's dis module lets you peek at the secret instructions your computer actually runs when it executes your Python code.
- Python Gc Module Internals How Python's garbage collector automatically cleans up memory you are no longer using — like a tidy roommate for your program.
- Python Importlib Custom Loaders How Python's importlib lets you teach Python to load code from anywhere — databases, zip files, the internet, or even generated on the fly.
- Python Startup Optimization Why Python takes a moment to start and what you can do to make your scripts and tools launch faster.