Missing imports
When you import a module, mypy may report that it is unable tofollow the import.
This can cause errors that look like the following:
- main.py:1: error: No library stub file for standard library module 'antigravity'
- main.py:2: error: Skipping analyzing 'django': found module but no type hints or library stubs
- main.py:3: error: Cannot find implementation or library stub for module named 'this_module_does_not_exist'
If you get any of these errors on an import, mypy will assume the type of thatmodule is Any
, the dynamic type. This means attempting to access anyattribute of the module will automatically succeed:
- # Error: Cannot find implementation or library stub for module named 'does_not_exist'
- import does_not_exist
- # But this type checks, and x will have type 'Any'
- x = does_not_exist.foobar()
The next three sections describe what each error means and recommended next steps.
Missing type hints for standard library module
If you are getting a “No library stub file for standard library module” error,this means that you are attempting to import something from the standard librarywhich has not yet been annotated with type hints. In this case, try:
Updating mypy and re-running it. It’s possible type hints for that cornerof the standard library were added in a newer version of mypy.
Filing a bug report or submitting a pull request totypeshed, the repository of type hintsfor the standard library that comes bundled with mypy.
Changes to typeshed will come bundled with mypy the next time it’s released.In the meantime, you can add a # type: ignore
to the import to suppressthe errors generated on that line. After upgrading, run mypy with the—warn-unused-ignores
flag to help youfind any # type: ignore
annotations you no longer need.
Missing type hints for third party library
If you are getting a “Skipping analyzing X: found module but no type hints or library stubs”,error, this means mypy was able to find the module you were importing, but nocorresponding type hints.
Mypy will not try inferring the types of any 3rd party libraries you have installedunless they either have declared themselves to bePEP 561 compliant stub package or have registeredthemselves on typeshed, the repositoryof types for the standard library and some 3rd party libraries.
If you are getting this error, try:
Upgrading the version of the library you’re using, in case a newer versionhas started to include type hints.
Searching to see if there is a PEP 561 compliant stub package.corresponding to your third party library. Stub packages let you installtype hints independently from the library itself.
For example, if you want type hints for the django
library, you caninstall the django-stubs package.
- Writing your own stub files containing type hints forthe library. You can point mypy at your type hints either by passingthem in via the command line, by using the
files
ormypy_path
config file options, or byadding the location to theMYPYPATH
environment variable.
These stub files do not need to be complete! A good strategy is to usestubgen, a program that comes bundled with mypy, to generate a firstrough draft of the stubs. You can then iterate on just the parts of thelibrary you need.
If you want to share your work, you can try contributing your stubs backto the library – see our documentation on creatingPEP 561 compliant packages.
If you are unable to find any existing type hints nor have time to write yourown, you can instead suppress the errors. All this will do is make mypy stopreporting an error on the line containing the import: the imported modulewill continue to be of type Any
.
To suppress a single missing import error, add a
# type: ignore
at the end of theline containing the import.To suppress all missing import imports errors from a single library, adda section to your mypy config file for that library setting
ignore_missing_imports
to True. For example, suppose your codebasemakes heavy use of an (untyped) library namedfoobar
. You can silenceall import errors associated with that library and that library alone byadding the following section to your config file:
- [mypy-foobar]
- ignore_missing_imports = True
Note: this option is equivalent to adding a # type: ignore
to everyimport of foobar
in your codebase. For more information, see thedocumentation about configuringimport discovery in config files.
- To suppress all missing import errors for all libraries in your codebase,invoke mypy with the
—ignore-missing-imports
command line flag or settheignoremissing_imports
config file option to Truein the _global section of your mypy config file:
- [mypy]
- ignore_missing_imports = True
We recommend using this approach only as a last resort: it’s equivalentto adding a # type: ignore
to all unresolved imports in your codebase.
Unable to find module
If you are getting a “Cannot find implementation or library stub for module”error, this means mypy was not able to find the module you are trying toimport, whether it comes bundled with type hints or not. If you are gettingthis error, try:
Making sure your import does not contain a typo.
If the module is a third party library, making sure that mypy is ableto find the interpreter containing the installed library.
For example, if you are running your code in a virtualenv, make sureto install and use mypy within the virtualenv. Alternatively, if youwant to use a globally installed mypy, set the—python-executable
commandline flag to point the Python interpreter containing your installedthird party packages.
Reading the How imports are found section below to make sure youunderstand how exactly mypy searches for and finds modules and modifyhow you’re invoking mypy accordingly.
Directly specifying the directory containing the module you want totype check from the command line, by using the
files
ormypy_path
config file options,or by using theMYPYPATH
environment variable.
Note: if the module you are trying to import is actually a submodule ofsome package, you should specific the directory containing the entire package.For example, suppose you are trying to add the module foo.bar.baz
which is located at ~/foo-project/src/foo/bar/baz.py
. In this case,you must run mypy ~/foo-project/src
(or set the MYPYPATH
to~/foo-project/src
.
- If you are using namespace packages – packages which do not contain
init.py
files within each subfolder – using the—namespace-packages
commandline flag.
In some rare cases, you may get the “Cannot find implementation or librarystub for module” error even when the module is installed in your system.This can happen when the module is both missing type hints and is installedon your system in a unconventional way.
In this case, follow the steps above on how to handlemissing type hints in third party libraries.