sys.path is an ordered list of directories Python searches when an import statement requests a module. Python uses the first matching module and raises ImportError when none matches. section 10.4
Python initializes sys.path from PYTHONPATH or installation defaults. A script adds its containing directory first; an interactive session uses an empty first element to represent the current directory. section 10.4
Make application modules discoverable by:
- Keeping modules beside the program that uses them. section 10.4.1
- Adding a shared site-specific directory to sys.path in code. section 10.4.1
- Setting PYTHONPATH or adding directories through a .pth file. section 10.4.1
- Installing third-party modules into a directory already included in the search path. section 10.6
Inspecting the search path
import sys sys.path The resulting list varies by installation, but its order determines which matching module Python imports first. section 10.4
import sys sys.path import sys sys.path ['/content', '/env/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.10/dist-packages/IPython/extensions', '/root/.ipython'] #1 #1 This listing will depend on your Python setup.
Inspect a concrete sys.path listing and observe how Python records installation directories, the current directory, and extension locations. section 10.4


