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.

Where to put Jinja extension code?

See original GitHub issue

Hi I put extension code in cookiecutter.json

{
"_extensions": ["super.SuperExtension"]
}

and I created the super/super.py and super/__init__.py files with:

from jinja2 import nodes
from jinja2.ext import Extension


class SuperExtension(Extension):
    # a set of names that trigger the extension.
    # tags = {'cache'}

    def __init__(self, environment):
        super(SuperExtension, self).__init__(environment)

        # add the defaults to the environment
        environment.extend(
            keep_trailing_newline=True
        )

However when I run the cookiecutter command it keeps telling me cookiecutter.exceptions.UnknownExtension: Unable to load extension: No module named 'super'

I copied the super directory everywhere I can think and it don’t find it. So my question is where do I need to put the super dir?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
con-f-usecommented, Apr 17, 2020

Just a quick related question

Why is it, that the cookiecutter template directory is not added to path by default or at least an extensions directory therein? Lets say we have this structure and I have a custom extension in my_custom_ext.py, that I’d like to use.

$ tree .
.
├── cookiecutter.json
├── {{cookiecutter.package_name}}
│   └── ...
├── extensions
│   └── my_cumstom_ext.py
└── hooks
    ├── post_gen_project.py
    └── pre_gen_project.py

$ cat cookiecutter.json
{
    "_extensions": ["extensions.my_custom_ext"]
}

I don’t want to tell my user to export PYTHONPATH=/path/to/./extensions or some such. They would expect that to work out of the box. Especially if the template comes from a git repository and the user would have to figure where exactly cookiecutter downloaded it to.

The reason can’t be security because hooks basically already allow code execution. It can’t be a maintenance burden on the cookiecutter team, because only advanced users would write their own extensions and they’d expect that they are responsible for what they write.

0reactions
vstepanovcommented, Apr 12, 2021

if I don’t run cookiecutter yet after its installation or remove ~/.cookiecutters/my-template/ and run PYTHONPATH=~/.cookiecutters/my-template/ cookiecutter https://github.com/.../my-template it is failing with Unable to load extension: No module named 'extensions'. For each next run it works fine and extensions can be imported. Any thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jinja - Visual Studio Marketplace
First, you will need to install Visual Studio Code 0.10 . In the command palette ( cmd-shift-p ) select Install Extension and choose...
Read more >
Extensions — Jinja Documentation (3.0.x)
Jinja supports extensions that can add extra filters, tests, globals or even extend the parser. The main motivation of extensions is to move...
Read more >
Visual Studio Code and Jinja templates - Stack Overflow
6 Answers 6 · Go to preferences >> settings >> type "file associations" in the search settings bar, click on "edit settings.json" ·...
Read more >
How to Write a Jinja2 Extension - Ron.sh
Extending Jinja2 can be started by subclassing the jinja2.ext.Extension class and implementing the parse method. To introduce a new tag, we need ...
Read more >
Primer on Jinja Templating - Real Python
Adjust Your Base Template; Extend Child Templates; Include a ... Source Code: Click here to download the source code that you'll use to ......
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