python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.2k stars 2.78k forks source link

mypy cannot find module installed as editable by pip #7508

Closed tsoernes closed 5 years ago

tsoernes commented 5 years ago

I have a module installed as editable by pip, i.e. pip install -e ... Mypy cannot import it, even when run as python -m mypy:

feed.py:10: error: Cannot find module named 'rsspull.article'
feed.py:11: error: Cannot find module named 'rsspull.feedparser_types'

~/.config/mypy/config:

[mypy]
mypy_path = '/home/torstein/code/fintechdb/pull/RSSPull/rsspull:/home/torstein/code/fintechdb/pull/RSSPull:/home/torstein/code/fintechdb/:/home/torstein/code/fintechdb/Ftools/ftools:/home/torstein/anaconda3/envs/rss/lib/python3.7/site-packages/'

Installing a module as editable creates an egg link in the virtual env, whatever that is:

$ fd rsspull ~/anaconda3/envs/rss                                                                                              
/home/torstein/anaconda3/envs/rss/lib/python3.7/site-packages/rsspull.egg-link
mypy               0.720     
mypy-extensions    0.4.1 

(I'm running mypy inside of the anaconda virtual env. Python is able to find the module)

ethanhs commented 5 years ago

Does the package have a py.typed file in it as PEP 561 specifies?

tsoernes commented 5 years ago

@ethanhs I created two blank files py.typed, one in RSSPull (top level directory, i.e. same level as setup.py and one in RSSPull/rsspull (same level as the source code)..

Mypy is still unable to import the rsspull module.

ethanhs commented 5 years ago

I don't think this should matter, but does the setup.py have a zip_safe=False?

ethanhs commented 5 years ago

Also could you try running python -m mypy.sitepkgs?

tsoernes commented 5 years ago

@ethanhs No to the former.

['/home/torstein/anaconda3/envs/rss/lib/python3.7/site-packages', '/home/torstein/.local/lib/python3.7/site-packages']
ethanhs commented 5 years ago

@tsoernes if you add zip_safe=False to your package, does it work? I can't remember, but I think that may fix it.

See the documentation too https://mypy.readthedocs.io/en/latest/installed_packages.html#making-pep-561-compatible-packages

tsoernes commented 5 years ago

It certainly does! Thanks a lot.

ethanhs commented 5 years ago

Great! Glad that worked. Maybe we should have a common issues section related to PEP 561 packages not working...