question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intermittent "circular import" when starting Django app

See original GitHub issue

Hi,

Thanks for a nice Python/Django package. It works fine, but sometimes I get this error on startup with this Django app installed (as described in the installation details):

pipenv run ./manage.py runserver
Watching for file changes with StatReloader
2021-04-20 13:37:09,008 INFO [django.utils.autoreload] Watching for file changes with StatReloader
Performing system checks...

Traceback (most recent call last):
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django_simple_bulma/templatetags/django_simple_bulma.py", line 11, in <module>
    from ..utils import (
ImportError: cannot import name 'fontawesome_token' from partially initialized module 'django_simple_bulma.utils' (most likely due to a circular import) (/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django_simple_bulma/utils.py)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/per/git/centraln/./manage.py", line 22, in <module>
    main()
  File "/home/per/git/centraln/./manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 61, in execute
    super().execute(*args, **options)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 96, in handle
    self.run(**options)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 103, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/utils/autoreload.py", line 637, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/utils/autoreload.py", line 622, in start_django
    reloader.run(django_main_thread)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/utils/autoreload.py", line 327, in run
    autoreload_started.send(sender=self)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 180, in send
    return [
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/autoreload.py", line 41, in watch_for_template_changes
    for directory in get_template_directories():
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/autoreload.py", line 14, in get_template_directories
    for backend in engines.all():
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django/template/backends/django.py", line 123, in get_package_libraries
    raise InvalidTemplateLibrary(
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django_simple_bulma.templatetags.django_simple_bulma': cannot import name 'fontawesome_token' from partially initialized module 'django_simple_bulma.utils' (most likely due to a circular import) (/home/per/.virtualenvs/centraln-czG2gyNf/lib/python3.9/site-packages/django_simple_bulma/utils.py)
make: *** [Makefile:24: serve] Error 1

Note that this only happens sometimes, which is both puzzling and annoying. It can also happen when making a change to any .py file in the project, i.e. when the apps are reloaded. Any ideas? Thanks in advance.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lemonsauruscommented, Jun 10, 2021

Haha, no problem. I appreciate your willingness to let us know!

0reactions
perluncommented, Jun 10, 2021

Heh. Please disregard that one. It was a genuine circular dependency in my own app… 🙈 🙈 🙈 😁 I found and fixed it now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Model imports failing between apps in django (circular imports?)
I am aware that this sort of thing can be the result of python preventing circular imports. Currently I have three django apps:...
Read more >
Avoiding circular dependencies | Django 3 Web Development ...
Circular dependencies are imports in different Python modules from each other. You should never cross-import from the different models.py files, because that ...
Read more >
[Solved]-why unable to import a model in django (most likely ...
Coding example for the question why unable to import a model in django (most likely due to a circular import)?-django.
Read more >
Debugging Haystack — Haystack 2.5.0 documentation
Double-check to ensure you have no circular imports. (i.e. module A tries importing from module B which is trying to import from module...
Read more >
Circular dependency problems when importing reversion on ...
On my server I'm running into an issue where reversion is unable to find different modules. Locally everything works fine yet on my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found