Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating Cookiecutter Extensions

I'm working on creating my first cookiecutter. By and large, this has gone well, but I now want to add a jinja2 filter of my own.

In line with the comments in this issue, I've created a new Jinja2 extension much like the one here. Full code for this extension is here:

https://github.com/seclinch/sigchiproceedings-cookiecutter/commit/5a314fa7207fa8ab7b4024564cec8bb1e1629cad#diff-f4acf470acf9ef37395ef389c12f8613

However, the following simple example demonstrates the same error:

# -*- coding: utf-8 -*-
from jinja2.ext import Extension


def slug(value):
    return value


class PaperTitleExtension(Extension):
    def __init__(self, environment):
        super(PaperTitleExtension, self).__init__(environment)
        environment.filters['slug'] = slug

I've dropped this code into a new jinja2_extensions directory and added a simple __init__.py as follows:

# -*- coding: utf-8 -*-
from paper_title import PaperTitleExtension

__all__ = ['PaperTitleExtension']

Based on this piece of documentation I've also added the following to my `cookiecutter.json' file:

"_extensions": ["jinja2_extensions.PaperTitleExtension"]

However, running this generates the following error:

$ cookiecutter sigchiproceedings-cookiecutter
Unable to load extension: No module named 'jinja2_extensions'

I'm guessing that I'm missing some step here, can anyone help?

like image 440
Saff Avatar asked Nov 18 '22 23:11

Saff


1 Answers

Had the same issue, try executing with python3 -m option

My extension in extensions/json_loads.py

import json

from jinja2.ext import Extension


def json_loads(value):
    return json.loads(value)


class JsonLoadsExtension(Extension):
    def __init__(self, environment):
        super(JsonLoadsExtension, self).__init__(environment)
        environment.filters['json_loads'] = json_loads

cookiecutter.json

{
  "service_name": "test",
  "service_slug": "{{ cookiecutter.service_name.replace('-', '_') }}",
...
  "_extensions": ["extensions.json_loads.JsonLoadsExtension"]
}

Then I executed with python3 -m cookiecutter . no_input=True timestamp="123" extra_dict="{\"features\": [\"redis\", \"grpc_client\"]}" -f and it works fine.

like image 82
onesiumus Avatar answered Dec 05 '22 14:12

onesiumus