Site and User Customization
Python provides a specific “hook” to let each site customize some aspects of Python’s behavior at the start of each run. Customization by each single user is not enabled by default, but Python specifies how programs that want to run user-provided code at startup can explicitly request such customization.
The site and sitecustomize Modules
Python loads standard module site just before the main script. If Python is run with option -S, Python does not load site. -S allows faster startup but saddles the main script with initialization chores. site’s tasks are:
Putting
sys.pathin standard form (absolute paths, no duplicates).Interpreting each .pth file found in the Python home directory, adding entries to
sys.path, and/or importing modules, as each .pth file indicates.Adding built-ins used to display information in interactive sessions (
quit,exit,copyright,credits, andlicense).Setting the default Unicode encoding to
'ascii'.site’s source code includes two blocks, each guarded byif 0:, one to set the default encoding to be locale-dependent, and the other to completely disable any default encoding between Unicode and plain strings. You may optionally edit site.py to select either block.Trying to import
sitecustomize(shouldimport sitecustomizeraise anImportErrorexception,sitecatches and ignores it).sitecustomizeis the module that each site’s installation can optionally use for further site-specific customization beyondsite’s tasks. It is generally ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access