On this page
site
Prev: signal | Up: stdlib_module | Next: smtplib
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — site
Tags: #public #python3 #stdlib
Import
import site
Overview
Startup configuration module automatically imported during Python initialization to populate sys.path with site-packages, process .pth files, detect virtual environments (sys.prefix vs sys.base_prefix), and register sitecustomize / usercustomize. Use site programmatically to query site-packages locations (getsitepackages(), getusersitepackages()) or add .pth-aware directories via addsitedir().
Quick Start
import site
# Inspect where third-party packages are installed for this interpreter
system_dirs = site.getsitepackages()
user_dir = site.getusersitepackages()
assert isinstance(system_dirs, list) and len(system_dirs) >= 1
assert isinstance(user_dir, str)
Implementation Walkthrough
1. Adding a Package Directory and Processing Its .pth Files
Unlike sys.path.append(), site.addsitedir() also evaluates any .pth files inside the added directory (needed for editable installs and namespace hooks):
import site
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
extra = Path(tmp) / "vendor"
extra.mkdir()
site.addsitedir(str(extra))
Gotchas & Boundaries
python3 -Sskipssite: Passing-Sto Python disables automaticsiteimport, sosite-packageswon't be onsys.path(andpython3 -I/-sdisables user site-packages).addsitedir()executesimportlines in.pthfiles: Only calladdsitedir()on trusted directories, because.pthfiles can run arbitrary Python code on lines starting withimport.- Inspect from the CLI: Running
python3 -m siteprintssys.path,USER_BASE,USER_SITE, and whether user site-packages are enabled—handy for debugging broken environments.
Common Functions
- site.abs_paths() —
abs_paths()— Set all module file and cached attributes to an absolute path - site.addpackage() —
addpackage(sitedir, name, known_paths)— Process a .pth file within the site-packages directory: For each line in the file, either combine it with sitedir to a path and add that to known_paths, or execute it if it starts with 'import '. - site.addsitedir() —
addsitedir(sitedir, known_paths=None)— Add 'sitedir' argument to sys.path if missing and handle .pth files in 'sitedir' - site.addsitepackages() —
addsitepackages(known_paths, prefixes=None)— Add site-packages to sys.path - site.addusersitepackages() —
addusersitepackages(known_paths)— Add a per user site-package to sys.path Each user has its own python directory with site-packages in the home directory. - site.check_enableusersite() —
check_enableusersite()— Check if user site directory is safe for inclusion The function tests for the command line flag (including environment var), process uid/gid equal to effective uid/gid. - site.enablerlcompleter() —
enablerlcompleter()— Enable default readline configuration on interactive prompts, by registering a sys.interactivehook. - site.execsitecustomize() —
execsitecustomize()— Run custom site specific code, if available. - site.execusercustomize() —
execusercustomize()— Run custom user specific code, if available. - site.gethistoryfile() —
gethistoryfile()— Check if the PYTHON_HISTORY environment variable is set and define it as the .python_history file. If PYTHON_HISTORY is not set, use the default .python_history file. - site.getsitepackages() —
getsitepackages(prefixes=None)— Returns a list containing all global site-packages directories. - site.getuserbase() —
getuserbase()— Returns theuser basedirectory path. - site.getusersitepackages() —
getusersitepackages()— Returns the user-specific site-packages directory path. - site.main() —
main()— Add standard site-specific directories to the module search path. - site.makepath() —
makepath(*paths)—site.makepath()function. - site.register_readline() —
register_readline()— Configure readline completion on interactive prompts. - site.removeduppaths() —
removeduppaths()— Remove duplicate entries from sys.path along with making them absolute - site.setcopyright() —
setcopyright()— Set 'copyright' and 'credits' in builtins - site.sethelper() —
sethelper()—site.sethelper()function. - site.setquit() —
setquit()— Define new builtins 'quit' and 'exit'. - site.venv() —
venv(known_paths)—site.venv()function.
Constants & Attributes
site.ENABLE_USER_SITE(bool) =Truesite.PREFIXES(list) =['/opt/homebrew', '/opt/homebrew/opt/python@3.14/Framewor...site.USER_BASE(str) ='/Users/curtis/.gemini/users/user1/Library/Python/3.14'site.USER_SITE(str) ='/Users/curtis/.gemini/users/user1/Library/Python/3.14/li...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/signal
- tuts/python3/stdlib_module/site/abs_paths
- tuts/python3/stdlib_module/site/addpackage
- tuts/python3/stdlib_module/site/addsitedir
- tuts/python3/stdlib_module/site/addsitepackages
- tuts/python3/stdlib_module/site/addusersitepackages
- tuts/python3/stdlib_module/site/check_enableusersite
- tuts/python3/stdlib_module/site/enablerlcompleter
- tuts/python3/stdlib_module/site/execsitecustomize
- tuts/python3/stdlib_module/site/execusercustomize
- tuts/python3/stdlib_module/site/gethistoryfile
- tuts/python3/stdlib_module/site/getsitepackages
- tuts/python3/stdlib_module/site/getuserbase
- tuts/python3/stdlib_module/site/getusersitepackages
- tuts/python3/stdlib_module/site/main
- tuts/python3/stdlib_module/site/makepath
- tuts/python3/stdlib_module/site/register_readline
- tuts/python3/stdlib_module/site/removeduppaths
- tuts/python3/stdlib_module/site/setcopyright
- tuts/python3/stdlib_module/site/sethelper
- tuts/python3/stdlib_module/site/setquit
- tuts/python3/stdlib_module/site/venv
- tuts/python3/stdlib_module/smtplib
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/signal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/abs_paths Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/addpackage Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/addsitedir Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/addsitepackages Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/addusersitepackages Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/check_enableusersite Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/enablerlcompleter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/execsitecustomize Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/site/execusercustomize Linked here; links to this page; shared tags: #stdlib