blob: d2420cfddea11a91832333c70f9199172677b3df [file] [log] [blame]
Guido van Rossume57c96e1996-08-17 19:56:26 +00001"""Hook to allow easy access to site-specific modules.
2
3Scripts or modules that need to use site-specific modules should place
4
5 import site
6
7somewhere near the top of their code. This will append up to two
8site-specific paths ($prefix/lib/site-python and
9$exec_prefix/lib/site-python) to the module search path. ($prefix
10and $exec_prefix are configuration parameters, and both default
11to /usr/local; they are accessible in Python as sys.prefix and
12sys.exec_prefix).
13
14Because of Python's import semantics, it is okay for more than one
15module to import site -- only the first one will execute the site
16customizations. The directories are only appended to the path if they
17exist and are not already on it.
18
19Sites that wish to provide site-specific modules should place them in
20one of the site specific directories; $prefix/lib/site-python is for
21Python source code and $exec_prefix/lib/site-python is for dynamically
22loadable extension modules (shared libraries).
23
24After these path manipulations, an attempt is made to import a module
25named sitecustomize, which can perform arbitrary site-specific
26customizations. If this import fails with an ImportError exception,
27it is ignored.
28
29Note that for non-Unix systems, sys.prefix and sys.exec_prefix are
30empty, and the path manipulations are skipped; however the import of
31sitecustomize is still attempted.
32
33XXX Any suggestions as to how to handle this for non-Unix systems???
34"""
35
36import sys, os
37
38for prefix in sys.prefix, sys.exec_prefix:
39 if prefix:
40 sitedir = os.path.join(prefix, os.path.join("lib", "site-python"))
41 if sitedir not in sys.path and os.path.isdir(sitedir):
42 sys.path.append(sitedir) # Add path component
43
44try:
45 import sitecustomize # Run arbitrary site specific code
46except ImportError:
47 pass # No site customization module