Search code examples
pythonjinja2openwisp

jinja2 TemplateNotFound if my library installed via pip


The following code is in a library

self.env = Environment(loader=PackageLoader('netjsonconfig.backends.openwrt', 'templates'),
                       trim_blocks=True)

The library is used by a consumer app, if the library is installed via python setup.py develop the consumer app works correctly while if the library is installed via pip the consumer app crashes with the following traceback (see failing build on travis):

Traceback (most recent call last):
      File "/home/travis/build/openwisp/django-netjsonconfig/django_netjsonconfig/tests/test_device_admin.py", line 33, in test_download_config
        response = self.client.get(path)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/test/client.py", line 503, in get
        **extra)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/test/client.py", line 304, in get
        return self.generic('GET', path, secure=secure, **r)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/test/client.py", line 380, in generic
        return self.request(**r)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/test/client.py", line 467, in request
        six.reraise(*exc_info)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/utils/six.py", line 686, in reraise
        raise value
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
        response = self.process_exception_by_middleware(e, request)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
        response = view_func(request, *args, **kwargs)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
        response = view_func(request, *args, **kwargs)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 244, in inner
        return view(request, *args, **kwargs)
      File "/home/travis/build/openwisp/django-netjsonconfig/django_netjsonconfig/admin.py", line 52, in download_view
        device.backend_instance.generate(device.name)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/netjsonconfig-0.3.1-py3.4.egg/netjsonconfig/backends/openwrt/openwrt.py", line 104, in generate
        uci = self.render()
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/netjsonconfig-0.3.1-py3.4.egg/netjsonconfig/backends/openwrt/openwrt.py", line 77, in render
        additional_output = renderer.render()
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/netjsonconfig-0.3.1-py3.4.egg/netjsonconfig/backends/base.py", line 41, in render
        template = self.env.get_template(template_name)
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/Jinja2-2.8-py3.4.egg/jinja2/environment.py", line 812, in get_template
        return self._load_template(name, self.make_globals(globals))
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/Jinja2-2.8-py3.4.egg/jinja2/environment.py", line 774, in _load_template
        cache_key = self.loader.get_source(self, name)[1]
      File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/Jinja2-2.8-py3.4.egg/jinja2/loaders.py", line 235, in get_source
        raise TemplateNotFound(template)
    jinja2.exceptions.TemplateNotFound: system.uci

It seems that it's looking templates in ./templates and it obviously doesn't work. I think I've not understood TemplateLoaders in Jinja very well. What am I doing wrong?


Solution

  • When you pip install netjsonconfig it does not include a templates folder along side the python code. Looks like a setup.py issue.

    (venv)Josh:netjsonconfig$ find .
    .
    ./__init__.py
    ./backends
    ./backends/__init__.py
    ./backends/base.py
    ./backends/openwisp
    ./backends/openwisp/__init__.py
    ./backends/openwisp/openwisp.py
    ./backends/openwisp/schema.py
    ./backends/openwrt
    ./backends/openwrt/__init__.py
    ./backends/openwrt/openwrt.py
    ./backends/openwrt/renderers.py
    ./backends/openwrt/schema.py
    ./backends/openwrt/timezones.py
    ./exceptions.py
    ./schema.py
    ./utils.py
    ./version.py