I'm encountering a bug with mypy telling me that it can't find a module that I know is installed.
The module in question is an internal library called mango, and the project I'm trying to use it in is called raptor. For mango, all functions are annotated and mypy passes with flying colors.
I am on macOS, Python 3.7, and using tox to run my tests. Below is the part of my tox.ini file that has the environment I've defined for mypy:
[testenv:type]
deps =
vendor/mango-0.4.3.tar.gz
mypy
commands =
python -c "import mango; print(mango.__file__)"
pip list
mypy --package raptor
Normally, under commands I only have the mypy executable line, but for this post I've added the other two lines just to prove that there should be no issues importing mango. Below is the error output from tox:
type installed: raptor==0.2.0,mango==0.4.3,mpmath==1.1.0,mypy==0.670,mypy-extensions==0.4.1,numpy==1.16.2,scipy==1.2.1,six==1.12.0,sympy==1.3,typed-ast==1.3.1,unyt==1.0.7
type run-test-pre: PYTHONHASHSEED='3632297771'
type runtests: commands[0] | python -c 'import mango; print(mango.__file__)'
/Users/username/projects/raptor/.tox/type/lib/python3.7/site-packages/mango/__init__.py
type runtests: commands[1] | pip list
Package Version
--------------- -------
raptor 0.2.0
mango 0.4.3
mpmath 1.1.0
mypy 0.670
mypy-extensions 0.4.1
numpy 1.16.2
pip 19.0.3
scipy 1.2.1
setuptools 40.8.0
six 1.12.0
sympy 1.3
typed-ast 1.3.1
unyt 1.0.7
wheel 0.33.1
type runtests: commands[2] | mypy --package raptor
raptor/__init__.py:5: error: Cannot find module named 'mango'
raptor/__init__.py:5: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
...
ERROR: InvocationError for command '/Users/username/projects/raptor/.tox/type/bin/mypy --package raptor' (exited with code 1)
commands failed
As you can see, Python is able to import my package, and pip also shows that it is there. pytest and sphinx are both able to import mango in their respective virtual environments. It's only mypy that is broken.
I'm not sure how to fix this issue because mypy is lying to me telling me that mango isn't installed. Since I can import it just fine, and it is installed into the virtualenv, I feel like I shouldn't have to modify MYPYPATH or anything.
The solution is to propose a PR to typeshed to add stubs for mango.
In future please don't assume the tool is broken unless you are very highly confident of it. Especially if you haven't looked carefully at the docs, and made sure you are using it correctly. The docs have a section exactly covering the "issue" you are reporting https://mypy.readthedocs.io/en/latest/installed_packages.html. Since this is an internal tool I'd recommend not putting the stubs in typeshed, and instead making a PEP 561 package.
I think the error message is misleading.
Cannot find module named 'mango' implies that mypy has no idea where mango is.
But AFAICT from the docs the real truth is that mypy does find mango, but mango is neither a PEP 561 nor has stubs in typeshed, so mypy cannot work with mango.
A better error message might be:
No type information available for module 'mango'
Most helpful comment
I think the error message is misleading.
Cannot find module named 'mango'implies thatmypyhas no idea wheremangois.But AFAICT from the docs the real truth is that
mypydoes findmango, butmangois neither a PEP 561 nor has stubs intypeshed, somypycannot work withmango.A better error message might be: