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 ...
Get Modular Programming with Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.