Unable to use jinja2 template in pip package created from python module
<p>I have a python project with the following directory structure</p>

<pre><code>foo
  |--MANIFEST.in
  |--requirements.txt
  |--setup.py
  |--foo.py
  |--templates/
     |--bar.tmpl
</code></pre>

<p>where <code>setup.py</code> is a python script. Previously, I was using an install script to symlink the script to my user's private <code>bin</code> (and using it with success), but decided to package it up (first time doing so). I'm able to successfully install the package in a virutalenv using <code>$ pip install .</code> from the project's root directory, and I am able to execute most of the script until I generate a template with <code>jinja2</code>. It seems as though either the <code>templates</code> dir is not getting installed with the rest of the package, or my script is not finding the path to the <code>templates</code> dir correctly.</p>

<p>Excerpt from <code>foo.py</code>:</p>

<pre><code>from jinja2 import Environment, ModuleLoader

def generate_readme(template_file):
    template_env = Environment(loader=PackageLoader('foo','templates'))
    template = template_env.get_template(template_file)        
    template_vars = {"title": get_title()}
    output = template.render(template_vars)
    return output
</code></pre>

<p><strong>Note</strong>: <code>'bar.tmpl'</code> is passed to this function as <code>template_file</code></p>

<p>Excerpt from <code>setup.py</code>:</p>

<pre><code>from setuptools import setup

setup(name='foo',                                                         
      py_modules=['foo'],
      entry_points={'console_scripts': ['foo = foo:foo']}          
      include_package_data=True,
      zip_safe=False)
</code></pre>

<p>Contents of <code>MANIFEST.in</code>:</p>

<pre><code>include templates/*
</code></pre>

<p>Relevant Traceback:</p>

<pre><code>  File "/home/username/.virtualenvs/foo/local/lib/python2.7/site-packages/foo.py", line 30, in generate_readme
template = template_env.get_template(template_file)
  File "/home/username/.virtualenvs/foo/local/lib/python2.7/site-packages/jinja2/environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
  File "/home/username/.virtualenvs/foo/local/lib/python2.7/site-packages/jinja2/environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
  File "/home/username/.virtualenvs/foo/local/lib/python2.7/site-packages/jinja2/loaders.py", line 235, in get_source
  raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: bar.tmpl
</code></pre>

<p>I have spent a few hours googling, reading similar stackoverflow threads, reading the jinja and setuptools docs, and reading similar code. Everything I have tested just results in the same error message, and I am somewhat at a loss. Any help is greatly appreciated.</p>

<p><code>MANIFEST.in</code> tell what files to include in the source distribution, i.e. <code>python setup.py sdist</code>, but it does not directly affect what files are installed because <code>pip install .</code> just calls into setuptools and doesn't do anything special with package_data.</p>

<p>You need to include the files in the setup.py file, either as package data or as additional files.</p>

<p>See <a href="https://docs.python.org/2/distutils/setupscript.html" rel="nofollow">https://docs.python.org/2/distutils/setupscript.html</a> for details, pay attention to <code>data_files</code> and <code>package_data</code> on the page.</p>

python python-2.7 pip jinja2 setuptools