question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MyPy Cannot Find Library Stubs for Specific Python Version

See original GitHub issue

Disclaimer: 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:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
navneethccommented, Nov 7, 2021

This is strange, I get the same set of errors as before: requests for 3.7 and 3.8, and click and click.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.

0reactions
cjolowiczcommented, Nov 1, 2021

Apparently mypy’s cache can mask import errors that would otherwise occur.

  1. Run nox against your HEAD: no errors
  2. Run nox against the parent of 382ed3a: import error for nox only
  3. Remove .mypy_cache
  4. Run nox against the same revision: import errors for nox, pytest, click, requests

I haven’t had time to look further into this. But if you experience this again, try removing .mypy_cache.

navneethc on  master is 📦 v0.1.0 via 🐍 v3.9.7
❯ nox -s mypy
nox > Running session mypy-3.8
nox > Creating virtual environment (virtualenv) using python3.8 in .nox/mypy-3-8
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmp_1i21cfz
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmp_1i21cfz 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 > Creating virtual environment (virtualenv) using python3.7 in .nox/mypy-3-7
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpkuby4iy6
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpkuby4iy6 mypy
nox > mypy src tests noxfile.py
Success: no issues found in 8 source files
nox > Session mypy-3.7 was successful.
nox > Ran multiple sessions:
nox > * mypy-3.8: success
nox > * mypy-3.7: success

navneethc on  master is 📦 v0.1.0 via 🐍 v3.9.7 took 25s
❯ git switch --detach 382ed3a4583f5c2d9c463178118115af05234e89~
HEAD is now at c826828 Add nox session for mypy

navneethc on  HEAD (c826828) is 📦 v0.1.0 via 🐍 v3.9.7
❯ nox -s mypy
nox > Running session mypy-3.8
nox > Creating virtual environment (virtualenv) using python3.8 in .nox/mypy-3-8
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmprur0bx8l
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmprur0bx8l mypy
nox > mypy src tests noxfile.py
noxfile.py:3: error: Cannot find implementation or library stub for module named "nox"
noxfile.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 8 source files)
nox > Command mypy src tests noxfile.py failed with exit code 1
nox > Session mypy-3.8 failed.
nox > Running session mypy-3.7
nox > Creating virtual environment (virtualenv) using python3.7 in .nox/mypy-3-7
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpnyut_65u
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpnyut_65u mypy
nox > mypy src tests noxfile.py
noxfile.py:3: error: Cannot find implementation or library stub for module named "nox"
noxfile.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (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: failed
nox > * mypy-3.7: failed

navneethc on  HEAD (c826828) is 📦 v0.1.0 via 🐍 v3.9.7 took 32s
❯ rm -rf .mypy_cache/

navneethc on  HEAD (c826828) is 📦 v0.1.0 via 🐍 v3.9.7
❯ nox -s mypy
nox > Running session mypy-3.8
nox > Creating virtual environment (virtualenv) using python3.8 in .nox/mypy-3-8
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpvwknoj3e
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpvwknoj3e mypy
nox > mypy src tests noxfile.py
tests/conftest.py:1: error: Cannot find implementation or library stub for module named "pytest"
src/my_hypermodern_python/wikipedia.py:1: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/wikipedia.py:2: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
src/my_hypermodern_python/wikipedia.py:2: note: Hint: "python3 -m pip install types-requests"
src/my_hypermodern_python/wikipedia.py:2: note: (or run "mypy --install-types" to install all missing stub packages)
noxfile.py:3: error: Cannot find implementation or library stub for module named "nox"
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"
tests/test_console.py:2: error: Cannot find implementation or library stub for module named "pytest"
tests/test_console.py:3: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
Found 9 errors in 5 files (checked 8 source files)
nox > Command mypy src tests noxfile.py failed with exit code 1
nox > Session mypy-3.8 failed.
nox > Running session mypy-3.7
nox > Creating virtual environment (virtualenv) using python3.7 in .nox/mypy-3-7
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpwufkbykd
nox > python -m pip install --constraint=/var/folders/r6/770jr8dd1_b7c63zbpnpy4gh0000gn/T/tmpwufkbykd mypy
nox > mypy src tests noxfile.py
tests/conftest.py:1: error: Cannot find implementation or library stub for module named "pytest"
src/my_hypermodern_python/wikipedia.py:1: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/wikipedia.py:2: error: Library stubs not installed for "requests" (or incompatible with Python 3.7)
src/my_hypermodern_python/wikipedia.py:2: note: Hint: "python3 -m pip install types-requests"
src/my_hypermodern_python/wikipedia.py:2: note: (or run "mypy --install-types" to install all missing stub packages)
noxfile.py:3: error: Cannot find implementation or library stub for module named "nox"
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"
tests/test_console.py:2: error: Cannot find implementation or library stub for module named "pytest"
tests/test_console.py:3: error: Library stubs not installed for "requests" (or incompatible with Python 3.7)
Found 9 errors in 5 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: failed
nox > * mypy-3.7: failed
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found