jinja2 TemplateNotFound 如果我的库是通过 pip 安装的

jinja2 TemplateNotFound if my library installed via pip

以下代码在a library

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

该库由消费者应用程序使用,如果该库是通过 python setup.py develop 安装的,则消费者应用程序可以正常工作,而如果该库是通过 pip 安装的,则消费者应用程序会崩溃并显示以下回溯(参见 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

好像是在./templates中查找模板,显然是行不通的。我想我还没有很好地理解 Jinja 中的 TemplateLoaders。我做错了什么?

当您 pip install netjsonconfig 时,它不包含 templates 文件夹以及 python 代码。看起来像一个 setup.py 问题。

(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