I am writing a simple Python application using FastAPI. I am using pdm as my package manager which means I have a pyproject.toml file and a pdm.lock file. I also using pyenv to manage both versions of Python installed on my system as well as virtual environments. So in this case, I started off creating my project by running commands like this:
I then followed the prompts that pdm gives to give my project a name, and install various dependencies (e.g. FastAPI, uvicorn, etc.). Now I'm trying to run mypy .
but it's complaining about basically every single import statement I have. Here's the full error text:
myproject/routers/transactions.py:1: error: Cannot find implementation or library stub for module named "asapi" [import-not-found]
myproject/routers/transactions.py:2: error: Cannot find implementation or library stub for module named "fastapi" [import-not-found]
myproject/routers/transactions.py:3: error: Cannot find implementation or library stub for module named "psycopg_pool" [import-not-found]
myproject/routers/system.py:1: error: Cannot find implementation or library stub for module named "fastapi" [import-not-found]
myproject/config/app.py:4: error: Cannot find implementation or library stub for module named "pydantic" [import-not-found]
myproject/config/app.py:5: error: Cannot find implementation or library stub for module named "pydantic_settings" [import-not-found]
tests/test_settings.py:3: error: Cannot find implementation or library stub for module named "pytest" [import-not-found]
tests/test_settings.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
myproject/config/loggers.py:7: error: Cannot find implementation or library stub for module named "orjson" [import-not-found]
myproject/config/loggers.py:8: error: Cannot find implementation or library stub for module named "opentelemetry.sdk.resources" [import-not-found]
myproject/config/loggers.py:9: error: Cannot find implementation or library stub for module named "opentelemetry.trace" [import-not-found]
myproject/main.py:5: error: Cannot find implementation or library stub for module named "anyio" [import-not-found]
myproject/main.py:6: error: Cannot find implementation or library stub for module named "asapi" [import-not-found]
myproject/main.py:7: error: Cannot find implementation or library stub for module named "fastapi" [import-not-found]
myproject/main.py:8: error: Cannot find implementation or library stub for module named "psycopg_pool" [import-not-found]
Found 14 errors in 6 files (checked 7 source files)
I am not sure how to overcome these errors. I know that I could simply create a mypy.ini file with this as its contents to effectively ignore these types of errors:
[mypy]
ignore_missing_imports = True
However, that seems like bad practice as I would much rather understand why mypy is showing these errors and how I can go about fixing the situation. My code is able to run successfully (thus demonstrating that the imports work), and if I run pip list
I can clearly see all of the dependencies installed within the virtual environment (which pyenv is managing). So why is mypy having so much trouble with these same imports?
As stated in the comments, if you use a globally installed mypy (e.g. with homebrew), you have to define the python executable to use with --python-executable
. According to mypy --help
:
--python-executable EXECUTABLE
Python executable used for finding PEP 561 compliant installed packages and stubs
If you are inside a virtual environment, you have to run something like:
mypy --python-executable venv/bin/python .
which is equivalent to:
mypy --python-executable python .