When you do import some_module, where does the python interpreter look for some_module.py? The answer is concretely in sys.path. sys.path is a list of paths that the python interpretter will look for modules in.

How does sys.path get populated? It’s populated from the following sources:

  • The directory that contains the “main” script being run
    • If you’re not running a script (e.g. you are using python interactively), then the CWD is used instead
  • The PYTHONPATH environment variable
  • The site-packages directory of the python installation (this is where pip installs 3rd party packages)
  • The standard library directory of the python installation (this is where the python standard library is installed)

You can also modify sys.path at runtime to add additional paths to look for modules in.