Where is the documentation page which explains how imports modify the sys.path?

I’m sure somewhere there is a documentation page which explains how the import statement modifies the sys.path variable depending on the form of the import.

For example, packages and modules have slightly different rules.

This is also related to executing a module, or executing a module inside a package with python3 -m. The two forms also change how sys.path is initialized.

If someone knows where this page is and could help me find it I would greatly appreciate it.

Thanks

The import statement does not modify sys.path, it just uses it.

The difference between the different methods to execute python is documented right by sys.path, but I agree that this information is a bit scattered. Most of your questions should be answered by 5. The import system — Python 3.12.4 documentation

Of course, the only caveat being that whatever you import is going to be run. If that module modifies the path, all bets are off.

If it seems like sys.path is being modified during imports, then I would be checking my dependencies in case somebody is doing something weird.

1 Like

I’m sure somewhere I have seen a table which describes how certain attributes are changed such as __package__, __path__, __name__ etc.

does anyone recall this?

5. The import system — Python 3.12.4 documentation probably this one? But that __path__ is not really related to sys.path

1 Like