MyPy Cannot Find Library Stubs for Specific Python Version
See original GitHub issueDisclaimer: This is not an issue per se, but a general question. I posted it to StackOverflow first, but there was no response, so I’m reposting it here in the hopes of finding answers.
I’m following the tutorial on type checking with mypy here.
When I run a nox session to run mypy against the code, the Python 3.7 check complains about mypy not being able to find stubs for some libraries (e.g. click) whereas the checks for Python 3.8 finish successfully. And until I ignored requests
, that was part of errors in 3.7 too. Why does this happen?
(my-hypermodern-python-VDazB31k-py3.8) ➜ nox -rs mypy
nox > Running session mypy-3.8
nox > Re-using existing virtual environment at .nox/mypy-3-8.
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/tmp/tmpyiz1np1u
nox > python -m pip install --constraint=/tmp/tmpyiz1np1u mypy
nox > mypy src tests noxfile.py
Success: no issues found in 8 source files
nox > Session mypy-3.8 was successful.
nox > Running session mypy-3.7
nox > Re-using existing virtual environment at .nox/mypy-3-7.
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/tmp/tmp7_a7k3nt
nox > python -m pip install --constraint=/tmp/tmp7_a7k3nt mypy
nox > mypy src tests noxfile.py
src/my_hypermodern_python/wikipedia.py:1: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/console.py:3: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/console.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
tests/test_console.py:1: error: Cannot find implementation or library stub for module named "click.testing"
tests/test_console.py:1: error: Cannot find implementation or library stub for module named "click"
Found 4 errors in 3 files (checked 8 source files)
nox > Command mypy src tests noxfile.py failed with exit code 1
nox > Session mypy-3.7 failed.
nox > Ran multiple sessions:
nox > * mypy-3.8: success
nox > * mypy-3.7: failed
WSL2 - Ubuntu 20.04 Python 3.7 and 3.8 versions from pyenv nox 2020.12.31 mypy 0.910 click 7.1.2
# noxfile.py
def install_with_constraints(session, *args, **kwargs):
with tempfile.NamedTemporaryFile() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
"--without-hashes",
f"--output={requirements.name}",
external=True,
)
session.install(f"--constraint={requirements.name}", *args, **kwargs)
# Nox session for mypy
@nox.session(python=["3.8", "3.7"])
def mypy(session):
args = session.posargs or locations
install_with_constraints(session, "mypy")
session.run("mypy", *args)
# mypy.ini
[mypy]
[mypy-nox.*,pytest,requests]
ignore_missing_imports = True
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
mypy Cannot find implementation or library stub for module
mypy has its own search path for imports and does not resolve imports exactly as Python does and it isn't able to find...
Read more >Running mypy and managing imports
If you are getting a Cannot find implementation or library stub for module error, this means mypy was not able to find the...
Read more >Find library stubs in __pypackages__ #10633 - python/mypy
It seems mypy cannot find library stubs when developing a project using PEP582, for example through PDM. Not opening as a bug but...
Read more >Missing types | Code Cookbook - Michael Currin
Write your own stub files containing type hints for the library. You can point mypy at your type hints either by passing them...
Read more >mypy library stubs not installed for - You.com | The AI Search ...
I'm running Mypy inside a gitlab CI/CD, and mypy can't find installed stubs, even though I've tried to use the --install-types option.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is strange, I get the same set of errors as before:
requests
for 3.7 and 3.8, andclick
andclick.testing
for 3.7 only, after deleting.mypy_cache
. I think I will set this aside this issue for the moment and explore in detail once I complete the tutorials. I will close this issue.Thanks so much for taking time in helping me out with this issue, and thank you also for creating this series of helpful blog posts.
Apparently mypy’s cache can mask import errors that would otherwise occur.
.mypy_cache
I haven’t had time to look further into this. But if you experience this again, try removing
.mypy_cache
.