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
fooare 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
foois found (i.e. a directory foo containing an__init__.pyor__init__.pyifile) that’s a match.- If a stub file named
foo.pyiis found, that’s a match.- If a Python module named
foo.pyis 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,
mypywill 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.pyfile in the root level). - You could add a
# type: ignoreto the import line. - You can edit the
MYPYPATHvariable to point to thefoodirectory:
(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