首页 > 解决方案 > Python3,jinja2,导入宏时出错

问题描述

我有以下python项目的结构:

myproject/
    __init__.py
    app.py
    static_sql.sql 
    templates/
        set_variables.sql

应用程序.py:

from jinja2 import Template, Environment, BaseLoader

with open("static_sql.sql") as f:
    query = f.read().strip()

tm = Environment(loader=BaseLoader()).from_string(query)
print(tm.render())

静态sql.sql:

{%- from 'templates/set_variables.sql' import define_total_counts with context %}


delete from {{ schema }}.realtime_feature_score
    where dw_date_key = {{ dw_date_key }}
      and feature_model_id = {{ model_id }};

set_variables.sql:

{% macro define_total_counts(include_cardinality=False, render=False) -%}


{%- endmacro -%}

当我运行app.py时出现错误:

jinja2.exceptions.TemplateNotFound:模板/set_variables.sql

我应该如何纠正这个错误?

标签: pythonmacrosjinja2

解决方案


推荐阅读