Sub topic 8
Submodules and dotted names
This chapter assumes you know how import and from ... import bind names, and that you have seen a package as a directory with an __init__.py file. It uses the shapes package from the previous chapter.
Dotted names mirror the directory tree
A submodule is a module that lives inside a package. Its full name joins each level with a dot. In the shapes package, the file shapes/util/mathx.py is the module shapes.util.mathx. The package shapes.util sits between them.
When you write import shapes.util.mathx, Python imports the parents first. It runs shapes/__init__.py, then shapes/util/__init__.py, then mathx.py. Each module goes into sys.modules. After a submodule loads, Python also sets it as an attribute on its parent. This is why shapes.util.mathx works as an expression.
import sys
import shapes.util.mathx
print(shapes.util.mathx.square(3))
print([name for name in sorted(sys.modules) if name.startswith("shapes")])This prints 9, then ['shapes', 'shapes.circle', 'shapes.util', 'shapes.util.mathx']. The shapes.circle entry appears because shapes/__init__.py imports it.
What each form binds
| Statement | Name bound in your module |
|---|---|
| `import shapes.util.mathx` | `shapes` only |
| `import shapes.util.mathx as m` | `m`, the `mathx` module |
| `from shapes.util import mathx` | `mathx` |
| `from shapes.util.mathx import square` | `square` |
The first form binds only the top-level name, so you must write the full path on every use. The as form binds the last module in the path, not the first.
Submodules are not loaded automatically
Importing a package does not import every file in it. import shapes runs only shapes/__init__.py. A submodule is available as an attribute only if something has already imported it. In our example, shapes/__init__.py imports .circle, and circle imports .util.mathx, so those happen to be loaded. If __init__.py were empty, shapes.circle would raise AttributeError until you ran import shapes.circle.
The form from shapes.util import mathx avoids this problem. If mathx is not yet an attribute of shapes.util, Python tries to import it as a submodule.
Every dotted part must be a module
In a plain import statement, each part before the last dot must be a package, and the last part must be a module. You cannot import a function this way.
import shapes.util.mathx.squareThis raises ModuleNotFoundError: No module named 'shapes.util.mathx.square'; 'shapes.util.mathx' is not a package. Use from shapes.util.mathx import square instead.
A misspelled part fails in the same way. import shapes.util.mathz raises ModuleNotFoundError: No module named 'shapes.util.mathz'. Read the error from the right: the name shown is the full dotted path that failed, so check that each directory has its __init__.py and that the file name matches.
Exercise
Given the shapes package above, after import shapes.util.mathx as m, is the name shapes bound in your module? How would you call square?
Show answer
No. The as form binds only m, so shapes is not a name in your module (though shapes, shapes.util, and shapes.util.mathx are all loaded in sys.modules). Call it as m.square(3).