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