I have a module (let's name it optional_module) that I want to be imported optionally, as it can be either present or absent. Now I do it this simple way:

try:
    import optional_module

except ImportError:
    ... # Working around the module

else:
    ... # Using the module

But mypy gives an error in case when optional_module is missing:

main.py:2: error: Cannot find implementation or library stub for module named
"optional_module"  [import-not-found]
        import optional_module
    ^
main.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

Yes, I looked at the link proposed by mypy, but it didn't help me to find the answer.

Of course, I could simply remove this check adding "# type: ignore" after the import line, but it would also suppress all useful checks of the module content in case when it does exist. So it's too rude.

Is there a more delicate and correct way to make mypy stop worrying and love my conditional import?

P.S. Here are my mypy configuration flags:

[mypy]
disallow_untyped_defs = True
check_untyped_defs = True
disallow_any_unimported = True
warn_return_any = True
warn_unreachable = True
warn_redundant_casts = True
warn_unused_ignores = True
pretty = True

4 Replies 4

You say

but it would also suppress all useful checks of the module content in case when it does exist

but I can't reproduce this (see mypy Playground):

import math  # type: ignore[import-not-found]

reveal_type(math.pi)  # Revealed type is "builtins.float"
math.typo  # Module has no attribute "typo"  [attr-defined]

Oops, you're right, thank you. I just assumed this behaviour of "#type: ignore" for some reason, and didn't even check it, shame on me. So, your answer has almost solved my problem. The only issue left is (in case of existing optional_module), mypy gives the following error:


main.py:2: error: Unused "type: ignore" comment  [unused-ignore]
        import optional_module # type: ignore[import-not-found]
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I see it's the result of enabled flag warn_unused_ignores in my config. I can just turn it off, but wouldn't like to do it globally. Is it possible to disable it just for my instance, without altering the global mypy INI file?

Sure, import optional_module # type: ignore[import-not-found, unused-ignore]

Yes, it works. Thank you, dROOOze

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.