You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suppress 'duplicate module named xx' errors? #4008
Am I right in thinking it's not presently possible to ignore errors for having multiple (as the error should really say, rather than 'duplicate') modules with the same name?
I'm running mypy on files in a repo that contains multiple packages with some commonality among them; for example they each have a src/wsgi.py, which renders:
error: Duplicate module named 'src.wsgi'
I think this should be reworded Multiple modules named xx
Would you be open to an --allow-same-name-modules/allow_same_name_modules = True or similar?
Reacted by Mohamed Laradji, František Lachman, 張泰瑋(Chang Tai Wei), Pavel Karateev, Hervé Saint-Amand, John Y. Davis, Tuukka Mustonen, Ninnart Fuengfusin, Elvis Stansvik, Ebram Shehata and 12 more
Those probably aren't packages, they're folders (or there are __init__.py files that shouldn't be there). You should really run mypy separately over each package in that case, since otherwise it won't know what to do when one of these is imported from another place being checked.
For this reason, implementing such a flag would not be easy, and probably not very useful.
Reacted by Mohamed Laradji, alper alimoglu, hasan salim kanmaz, Yoichiro Shimizu, Paweł Nadolski, Pablo Ambrosio, Kazuya Nishi, Mohammed dahi, Jeff Gruenbaum, Matthew Gamble and 1 more
Hi @gvanrossum, I am writing a PyCharm/IntelliJ IDEA plugin to run mypy scanning and I am encountering the same issue.
PyCharm/IntelliJ IDEA code inspection API sends me a list of open files in the IDE but, if the file list contains both a __init__.py and a python file from the same package, I get the "Duplicate module named" error:
This is because apparently you seem to have both a folder named gsi and a file name gsi.py. This is a problem for Python too (IIRC the folder/package wins) so you can't really blame mypy for complaining about this.
I have this problem frequently where I'm scanning separate packages with pre-commit hooks. It would be very useful to be able to disable this check completely, or else have an obvious workaround.
Scripts-are-modules does not help.
Reacted by Christopher Keele, František Lachman, Matthew Planchard, rachmadani haryono, Hervé Saint-Amand, Pierre Sassoulas, Patrick Arminio, Tuukka Mustonen, Hendrik Wiese, Dimitri Kokkonis and 19 more
That's exactly when this problem occurs - when that full list of files contains 'duplicate modules named x'.
(An example of the script that does that is pre-commit --all-files.)
Reacted by bcb, Malar Kannan, Silvio Tomatis, Brendan Long, tvbox41986, Hervé Saint-Amand, Pierre Sassoulas, Kyle Barron, Tuukka Mustonen, Augusto Hack and 4 more
First of all, specify pyproject.toml/mypy.ini for each of your sub-repo. You're likely to have different configs for each project.
For mypy specifically - make sure you're using mypy installed in your local environment. By default pre-commit runs mypy in isolated environment that doesn't have all the info about other packages (like pydantic, sqlalchemy, etc.) and therefore will make mistakes. This shouldn't be an issue if you work in monorepo environment.
This solution assumes that each sub-repo in monorepo has a sub-root directory under root (/project1, /project2, etc). SUBREPO is exactly this - the name of your project.
ABS_PATH - absolute path to the file that will be checked, to avoid duplicate module named.
We cd to the sub-repo directory and run mypy here on absolute path. Here is where your mypy config should be, if you share configuration among other sub-repos use --config-file ../.mypy.ini or similar. Use other args and flags as you wish.
Now mypy should have correct idea of what is going on. I did the same trick with bandit. It works correctly even if you want to commit edits on few projects in your monorepo. This solution my not be "clean", but works. It's the best I could come up with and pre-commit maintainer have said his word about supporting monorepos, so here we are.
8 years since the issue was created, mypy still doesn't support PEP 420-based implicit namespace packages properly. We are forced to either:
put __init__.py file in the top-level package - which is against PEP 420
modify mypy_path - which is unmaintainable (unless you create mypy_path generator...) in a setup with dynamic list of packages (e.g. monorepo)
It could be easily improved if mypy_path supports glob, but for whatever reason - it still hasn't been implemented (although we have an issue for that for 4 years): #9965. It's still not the final solution for the PEP 420 support*, but it would help a lot of people dealing with this problem.
*In terms of that - if mypy is able to identify packages automagically based on __init__.py discovery, I don't understand why we can't extend this approach. Instead of looking for __init__.py , let us specify what to look for when traversing through parent directories. In many cases it could be src/ directory which would mean:
if you find src/ directory when traversing, treat every direct subdirectory of src/ as a top-level package
and that should be able to handle both normal packages and namespace packages, as we don't really on __init__.py anyhow. Of course - it's still possible to use src/ directory not for packages, that's why it should be configurable. The thing is - usually some convention is established anyway (and the one with src/ for packages is pretty common nowadays), so we could use this fact to make our life with mypy much easier.
For the ones looking for a mypy config in pre-commit working with namespace packages in monorepos - the below is sufficient for our convention, you can always modify find command to fit your use-case:
[tool.mypy]
explicit_package_bases = trueignore_missing_imports = truefollow_imports = "silent"# not happy about this one, but afaik mypy doesn't allow to exclude some packages from "follow_imports"check_untyped_defs = truescripts_are_modules = true
Activity
gvanrossum commentedon Sep 25, 2017
Those probably aren't packages, they're folders (or there are
__init__.pyfiles that shouldn't be there). You should really run mypy separately over each package in that case, since otherwise it won't know what to do when one of these is imported from another place being checked.For this reason, implementing such a flag would not be easy, and probably not very useful.
leinardi commentedon Sep 1, 2018
Hi @gvanrossum, I am writing a PyCharm/IntelliJ IDEA plugin to run mypy scanning and I am encountering the same issue.
PyCharm/IntelliJ IDEA code inspection API sends me a list of open files in the IDE but, if the file list contains both a
__init__.pyand a python file from the same package, I get the "Duplicate module named" error:Do you have any suggestion to scan both this files without running mypy separately for each of them?
ilevkivskyi commentedon Sep 1, 2018
Would you like to contribute to existing one instead? https://github.com/dropbox/mypy-PyCharm-plugin
leinardi commentedon Sep 1, 2018
Hi @ilevkivskyi, normally that would be the best thing to do, but I have already done adapting the code of my Pylint plugin to work with mypy.
This plugin is based on Checkstyle-IDEA plugin and offers also real-time scanning, checkin validations and various on-demand scan actions.
This is my latest build, is still not production ready but should give you an idea of what feature are there: Mypy-0.7.0.zip
gvanrossum commentedon Sep 1, 2018
This is because apparently you seem to have both a folder named
gsiand a file namegsi.py. This is a problem for Python too (IIRC the folder/package wins) so you can't really blame mypy for complaining about this.leinardi commentedon Sep 1, 2018
Oh ok, thanks for the clarification 👍.
leinardi commentedon Sep 1, 2018
Hi @gvanrossum, I am now getting the same error when I try to check these files from mypy project:
or just simply these 2 files:
Is it also in this case a project problem? And why it does not happen when I scan the entire project using
mypy /home/leinardi/PycharmProjects/mypy/?gvanrossum commentedon Sep 1, 2018
Presumably the folders containing the
setup.pyfiles don't have__init__.pyfiles, so mypy treats them both as top-level modules. You can fix this using--scripts-are-modules: https://mypy.readthedocs.io/en/latest/command_line.html#miscellaneousleinardi commentedon Sep 2, 2018
Hey @gvanrossum, thank you for your answer. You are correct, the folders don't have
__init__.pyfiles.But unfortunately the option
--scripts-are-modulesis not fixing the issue:gvanrossum commentedon Sep 2, 2018
leinardi commentedon Sep 2, 2018
Done, but still no answer:

But isn't this a mypy bug? why I cannot scan these 2 files together but it is fine if I scan the entire directory with
mypy .?petergaultney commentedon Oct 31, 2018
I have this problem frequently where I'm scanning separate packages with pre-commit hooks. It would be very useful to be able to disable this check completely, or else have an obvious workaround.
Scripts-are-modules does not help.
gvanrossum commentedon Oct 31, 2018
You'll have to write a script that scans the packages and produces a full list of files to pass to mypy except for the files you don't want.
OJFord commentedon Oct 31, 2018
That's exactly when this problem occurs - when that full list of files contains 'duplicate modules named x'.
(An example of the script that does that is
pre-commit--all-files.)17 remaining items
Checks only platotk with mypy
Checks only platotk with mypy
Checks only platotk with mypy
Checks only platotk with mypy
Check only platotk with mypy
Check only platotk with mypy
Check only platotk with mypy
Check only platotk with mypy
nhtgl commentedon Jul 5, 2023
To those that are still looking for some solution for monorepo:
pyproject.toml/mypy.inifor each of your sub-repo. You're likely to have different configs for each project.duplicate module named.--config-file ../.mypy.inior similar. Use other args and flags as you wish.Now mypy should have correct idea of what is going on. I did the same trick with
bandit. It works correctly even if you want to commit edits on few projects in your monorepo. This solution my not be "clean", but works. It's the best I could come up with and pre-commit maintainer have said his word about supporting monorepos, so here we are.I hope that some of you will find it useful.
Use ruff, remove pyflakes and isort, and move black to the project root
Use ruff, remove pyflakes and isort, and move black to the project root
Soneji commentedon Sep 5, 2023
My solution was to add this option in the mypy configuration:
Removes optimization for https://github.com/python/mypy/issues/4008\#…
jaklan commentedon Mar 22, 2025
8 years since the issue was created,
mypystill doesn't support PEP 420-based implicit namespace packages properly. We are forced to either:__init__.pyfile in the top-level package - which is against PEP 420mypy_path- which is unmaintainable (unless you createmypy_pathgenerator...) in a setup with dynamic list of packages (e.g. monorepo)It could be easily improved if
mypy_pathsupportsglob, but for whatever reason - it still hasn't been implemented (although we have an issue for that for 4 years): #9965. It's still not the final solution for the PEP 420 support*, but it would help a lot of people dealing with this problem.*In terms of that - if
mypyis able to identify packages automagically based on__init__.pydiscovery, I don't understand why we can't extend this approach. Instead of looking for__init__.py, let us specify what to look for when traversing through parent directories. In many cases it could besrc/directory which would mean:and that should be able to handle both normal packages and namespace packages, as we don't really on
__init__.pyanyhow. Of course - it's still possible to usesrc/directory not for packages, that's why it should be configurable. The thing is - usually some convention is established anyway (and the one withsrc/for packages is pretty common nowadays), so we could use this fact to make our life withmypymuch easier.jaklan commentedon Mar 23, 2025
For the ones looking for a
mypyconfig inpre-commitworking with namespace packages in monorepos - the below is sufficient for our convention, you can always modifyfindcommand to fit your use-case: