mypy has its own search path for imports and does not resolve imports exactly as Python does and it isn't able to find the baz.alice
module. Check the documentation listed in the error message, specifically the section on How imports are found:
The rules for searching for a module foo
are as follows:
The search looks in each of the directories in the search path (see
above) until a match is found.
- If a package named
foo
is found (i.e. a directory foo containing an __init__.py
or __init__.pyi
file) that’s a match.
- If a stub file named
foo.pyi
is found, that’s a match.
- If a Python module named
foo.py
is found, that’s a match.
The documentation also states that this in the section on Mapping file paths to modules:
For each file to be checked, mypy
will attempt to associate the file
(e.g. project/foo/bar/baz.py
) with a fully qualified module name (e.g.
foo.bar.baz
).
There's a few ways to solve this particular issue:
- As paul41 mentioned in his comment, one option to solve this issue is by providing the fully qualified import (
from foo.baz.alice import Alice
), and then running from a top-level module (a .py
file in the root level).
- You could add a
# type: ignore
to the import line.
- You can edit the
MYPYPATH
variable to point to the foo
directory:
(venv) (base) ➜ mypy foo/bar.py --strict
foo/bar.py:3: error: Cannot find implementation or library stub for module named "baz.alice"
foo/bar.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
(venv) (base) ➜ export MYPYPATH=foo/
(venv) (base) ➜ mypy foo/bar.py --strict
Success: no issues found in 1 source file