Circular Imports

K-TPL-003 — Circular macro import detection

1 min read 186 words

When using{% from "template.html" import macro_name %}, a template must not import from itself (directly or transitively). Kida detects this and raises TemplateRuntimeError with code K-TPL-003instead of hitting Python's recursion limit.

The Error

TemplateRuntimeError: Circular import detected: 'showcase/_tab_content.html' imports itself (via showcase/_tab_content.html)
  Location: showcase/_tab_content.html:1
  Code: K-TPL-003

Cause

A template has{% from "X" import y %}where X is the same template or leads back to it:

{# BAD: _tab_content.html imports from itself #}
{% from "showcase/_tab_content.html" import tab_section %}
{% def tab_section() %}content{% end %}

Or transitive: A imports B, B imports A.

Fix

Split macros into a separate file and import from there:

{# _tab_section.html — macros only #}
{% def tab_section() %}content{% end %}
{# _tab_content.html — imports from sibling #}
{% from "showcase/_tab_section.html" import tab_section %}
{% block tab_content %}
  {{ tab_section() }}
{% end %}

See Also