Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
mypy Cannot find implementation or library stub for module

I have:

foo/
├── __init__.py
├── bar.py
└── baz
    ├── __init__.py
    └── alice.py

In bar.py, I import Alice, which is an empty class with nothing in it but the name attribute set to "Alice".

from baz.alice import Alice

a = Alice()
print(a.name)

This runs properly:

$ python foo/bar.py
Alice

But mypy complains:

$ mypy --version
mypy 0.910
$ mypy --strict .
foo/bar.py:1: error: Cannot find implementation or library stub for module named "baz.alice"
foo/bar.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 6 source files)

Why is mypy complaining?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

  1. If a package named foo is found (i.e. a directory foo containing an __init__.py or __init__.pyi file) that’s a match.
  2. If a stub file named foo.pyi is found, that’s a match.
  3. 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:

  1. 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).
  2. You could add a # type: ignore to the import line.
  3. 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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda