Skip to content

bug: Legacy python handler fails to setup Django starting with mkdocstrings 0.28+ #766

@mschoettle

Description

@mschoettle

Description of the bug

I am currently using mkdocstrings 0.27.0 with the python-legacy extra (0.2.4).

Starting with 0.28.0 I am getting the error:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must
either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

(see full stacktrace below)

I've tried upgrading the legacy python handler to 0.2.5 (and mkdocstrings 0.28.3) which fails with ModuleNotFoundError: No module named 'mkdocstrings_handlers'

To Reproduce

I can provide a full MRE if needed.

The config looks as follows:

- mkdocstrings:
      default_handler: python
      handlers:
        python:
          setup_commands:
            - import os
            - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.development")
            - import django
            - django.setup()
            # mock pytkdocs to workaround Django and Django REST framework issues
            - import docs.mock_django

Full traceback

Full traceback
ERROR   -  mkdocstrings: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment
           variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
           Traceback (most recent call last):
             File "<path>/.venv/lib/python3.12/site-packages/pytkdocs/cli.py",
           line 216, in main
               output = json.dumps(process_json(line))
                                   ^^^^^^^^^^^^^^^^^^
             File "<path>/.venv/lib/python3.12/site-packages/pytkdocs/cli.py",
           line 128, in process_json
               return process_config(json.loads(json_input))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             File "<path>/.venv/lib/python3.12/site-packages/pytkdocs/cli.py",
           line 106, in process_config
               obj = loader.get_object_documentation(path, members)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             File
           "<path>/.venv/lib/python3.12/site-packages/pytkdocs/loader.py", line
           336, in get_object_documentation
               leaf = get_object_tree(dotted_path, self.new_path_syntax)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             File
           "<path>/.venv/lib/python3.12/site-packages/pytkdocs/loader.py", line
           248, in get_object_tree
               parent_module = importlib.import_module(parent_module_path)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             File
           "/opt/homebrew/Cellar/python@3.12/3.12.10/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py",
           line 90, 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 1331, in _find_and_load_unlocked
             File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
             File "<frozen importlib._bootstrap_external>", line 999, in exec_module
             File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
             File "<path>/opal/conftest.py", line 13, in <module>
               from django.contrib.auth.models import Group, Permission
             File
           "<path>/.venv/lib/python3.12/site-packages/django/contrib/auth/models.py",
           line 3, in <module>
               from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
             File
           "<path>/.venv/lib/python3.12/site-packages/django/contrib/auth/base_user.py",
           line 57, in <module>
               class AbstractBaseUser(models.Model):
             File
           "<path>/.venv/lib/python3.12/site-packages/django/db/models/base.py",
           line 129, in __new__
               app_config = apps.get_containing_app_config(module)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
             File
           "<path>/.venv/lib/python3.12/site-packages/django/apps/registry.py",
           line 260, in get_containing_app_config
               self.check_apps_ready()
             File
           "<path>/.venv/lib/python3.12/site-packages/django/apps/registry.py",
           line 137, in check_apps_ready
               settings.INSTALLED_APPS
             File
           "<path>/.venv/lib/python3.12/site-packages/django/conf/__init__.py",
           line 102, in __getattr__
               self._setup(name)
             File
           "<path>/.venv/lib/python3.12/site-packages/django/conf/__init__.py",
           line 82, in _setup
               raise ImproperlyConfigured(
           django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must
           either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

And

Traceback (most recent call last):
  File "<path>/.venv/bin/mkdocs", line 10, in <module>
    sys.exit(cli())
             ^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/click/core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/click/core.py", line 1082, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/click/core.py", line 1697, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/click/core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/click/core.py", line 788, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/mkdocs/__main__.py", line 288, in build_command
    build.build(cfg, dirty=not clean)
  File "<path>/.venv/lib/python3.12/site-packages/mkdocs/commands/build.py", line 265, in build
    config = config.plugins.on_config(config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/mkdocs/plugins.py", line 587, in on_config
    return self.run_event('config', config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/mkdocs/plugins.py", line 566, in run_event
    result = method(item, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/mkdocstrings/_internal/plugin.py", line 149, in on_config
    handlers._download_inventories()
  File "<path>/.venv/lib/python3.12/site-packages/mkdocstrings/_internal/handlers/base.py", line 713, in _download_inventories
    handler = self.get_handler(handler_name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/.venv/lib/python3.12/site-packages/mkdocstrings/_internal/handlers/base.py", line 672, in get_handler
    module = importlib.import_module(f"mkdocstrings_handlers.{name}")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.10/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, 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 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  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 'mkdocstrings_handlers'

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions