django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

unknown import causes ModuleNotFoundError

Open leos opened this issue 5 years ago • 10 comments

Bug report

What's wrong

This plugin crashes mypy when there is an unknown import in a source file. Repro is super simple, add from foo import bar to source file, run mypy against it with this plugin.

Actual behavior: uncaught ModuleNotFoundError

Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 89, in main
  File "mypy/build.py", line 166, in build
  File "mypy/build.py", line 213, in _build
  File "mypy/build.py", line 451, in load_plugins
  File "mypy/build.py", line 429, in load_plugins_from_config
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/mypy_django_plugin/main.py", line 80, in __init__
    self.django_context = DjangoContext(django_settings_module)
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/mypy_django_plugin/django/context.py", line 88, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/mypy_django_plugin/django/context.py", line 72, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/leo/Library/Caches/pypoetry/virtualenvs/sprinty-PROX8LVJ-py3.7/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/leo/test.py", line 7, in <module>
    from foo import bar
ModuleNotFoundError: No module named 'foo'

How is that should be

Expected behavior: mypy works properly and plugin doesn't crash it.

System information

  • OS: MacOS 10.15.3
  • python version: 3.7.6
  • django version: 3.0.3
  • mypy version: 0.761
  • django-stubs version: 1.4.0

leos avatar Feb 15 '20 02:02 leos

This is a known problem. This happens because we run django.setup() inside the plugin. And if your code is broken - the plugin will crash. https://github.com/typeddjango/django-stubs#mypy-crashes-when-i-run-it-with-this-plugin-installed

I guess the only thing we can do is to make better error messages. I will work on it!

sobolevn avatar Feb 15 '20 05:02 sobolevn

Thanks for looking at this. I haven't looked deeper at the code but could you just catch the exception, create a mypy warning/error and then simply continue on? The plugin would be effectively disabled until the error was fixed but it wouldn't crash mypy.

leos avatar Feb 15 '20 11:02 leos

Yes, this is exactly my plan. By the way, @leos do you want to take this one? I can reassign the issue if you want.

sobolevn avatar Feb 15 '20 13:02 sobolevn

I appreciate the offer but I unfortunately don't have the time to dig in right now

leos avatar Feb 16 '20 15:02 leos

I see this is still open, and exists in many different forms across Django mypy and other Django plugins. I get a similar error, using UV:

I suppose it's related to the interaction between virtual environments, python's import system and mypy. Happy to attempt a mypy related fix at least - I wouldn't have the time to get into the internals of Django, and fixing python's import system is probably impossible!

As this has been open for ages, and appears in quite a few versions I guess people have tried and not found an easy fix. Please point me to your attempts if you've had a go!

For ref I'm using

  • Python 3.13
  • Django 5.1.3
  • UV 0.6.12
  • Mypy 1.1.3
Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "/Users/garvan/dev/liv2/backend/.venv/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "mypy/main.py", line 102, in main
  File "mypy/main.py", line 186, in run_build
  File "mypy/build.py", line 194, in build
  File "mypy/build.py", line 239, in _build
  File "mypy/build.py", line 504, in load_plugins
  File "mypy/build.py", line 485, in load_plugins_from_config
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/mypy_django_plugin/main.py", line 71, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
                          ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/mypy_django_plugin/django/context.py", line 130, in __init__
    apps, settings = initialize_django(self.django_settings_module)
                     ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/mypy_django_plugin/django/context.py", line 82, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/garvan/dev/liv/backend/.venv/lib/python3.13/site-packages/django/apps/config.py", line 193, in create
    import_module(entry)
    ~~~~~~~~~~~~~^^^^^^^
  File "/usr/local/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'knox'

garvanwalshe avatar Sep 02 '25 06:09 garvanwalshe

I see this is still open, and exists in many different forms across Django mypy and other Django plugins. I get a similar error, using UV:

@garvanwalshe And this is not really a django-stubs issue, you appear to have 'knox' listed in your settings.INSTALLED_APPS but don't have the package installed in your virtualenv. You should have the same issue with python manage.py runserver

Make sure django-rest-knox is in your dependencies and the error should be resolved.

We could improve the error django-stubs resurface in such cases but it would not solve the underlying issue and would disable the plugin, probably leading to a bunch of others incorrect mypy errors.

UnknownPlatypus avatar Sep 02 '25 08:09 UnknownPlatypus

Unfortunately not... Knox is installed in the app, through uv, and the actual app works. The issue has to do with mypy not finding plugins that Django does find.

Here's the pyproject.toml:

 dependencies = [
    "black>=24.10.0",
    "dj-database-url>=2.3.0",
    "django-ckeditor-5>=0.2.15",
    "django-filter>=24.3",
    "django-request-logging>=0.7.5",
    "django-rest-framework>=0.1.0",
    "django-rest-knox>=5.0.2",
    "django-sass>=1.1.0",
    "django-stubs[compatible-mypy]>=5.1.1",
    "djangorestframework>=3.15.2",
    "djangorestframework-stubs[compatible-mypy]>=3.15.2",
    "mypy>=1.13.0",
    "pylsp-mypy>=0.6.9",
    "python-decouple>=3.8",
    
]

garvanwalshe avatar Sep 02 '25 08:09 garvanwalshe

This is surprising because dhango-stubs uses runserver helpers to configure django. Can you tell us more about how you are running mypy ? How is it different from a regular runserver in your setup ?

UnknownPlatypus avatar Sep 02 '25 09:09 UnknownPlatypus

I run everything inside UVSo “uv run mypy”(Or through Helix’s mypy lsp - but I run helix through uv otherwise it doesn’t find anything!)Running it through helix provides the same error (not surprisingly) Sent from my iPhoneOn 2 Sep 2025, at 11:34, Thibaut Decombe @.***> wrote:UnknownPlatypus left a comment (typeddjango/django-stubs#329) This is surprising because dhango-stubs uses runserver helpers to configure django. Can you tell us more about how you are running mypy ? How is it different from a regular runserver in your setup ?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

garvanwalshe avatar Sep 02 '25 09:09 garvanwalshe

You might be facing this UV issue -> https://github.com/astral-sh/uv/issues/15334

Could you try running mypy without uv ? Or recreating your venv?

Otherwise I'll probably need a minimal reproduction repo to help you more

UnknownPlatypus avatar Sep 02 '25 11:09 UnknownPlatypus