Tweaking imports using sys.path
When you use the import
command, the Python interpreter has to search for the module or package you want to import. It does this by looking through the
module search path, which is a list of the various directories where modules or packages can be found. The module search path is stored in sys.path
, and the Python interpreter will check the directories in this list one after another until the desired module or package is found.
When the Python interpreter starts, it initializes the module search path with the following directories:
The directory containing the currently-executing script, or the current directory if you are running the Python interactive interpreter in a terminal window
Any directories listed in the
PYTHONPATH
environment variableThe contents of the interpreter's
site-packages
directory, including any modules referred to by path configuration files within thesite-packages
directoryNote
The
site-packages
directory is used to hold the various third...