Ignore import errors in API URLs check
While working on !1923 (merged), I shuffled some serializers around as they were in the wrong file. I expected to be able to fix the imports by just letting pre-commit
cry, then letting manage.py runserver
cry, but instead I got this:
λ ./manage.py runserver
2023-02-21 14:07:23,297 [INFO] Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/lucidiot/.virtualenvs/backend/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/lucidiot/.virtualenvs/backend/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
self.check(display_num_errors=True)
File "/home/lucidiot/.virtualenvs/backend/lib/python3.8/site-packages/django/core/management/base.py", line 475, in check
all_issues = checks.run_checks(
File "/home/lucidiot/.virtualenvs/backend/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/lucidiot/dev/ark/backend/arkindex/project/checks.py", line 41, in api_urls_check
from arkindex.project.api_v1 import api
ImportError: cannot import name 'api' from 'arkindex.project.api_v1' (/home/lucidiot/dev/ark/backend/arkindex/project/api_v1.py)
There was nothing wrong in arkindex.project.checks
or in arkindex.project.api_v1
though! I had some import errors in arkindex.documents.serializers.entities
instead.
This non-explicit error only occurs when you are importing from a module that does exist, but importing something that doesn't, for example from arkindex import beer
(sadly). Importing from a module that does not exist results in a ModuleNotFoundError
that will show the proper stack trace.
Handling the ImportError
when the API URLs check tries to import api_v1
to check the URLs allows Django to continue to load after running the system checks, which will end up making it crash once more with the real ImportError
, showing the proper stack trace that points at the real import issue.