Q:

Unable to use jinja2 template in pip package created from python module

I have a python project with the following directory structure
foo
  |--MANIFEST.in
  |--requirements.txt
  |--setup.py
  |--foo.py
  |--templates/
     |--bar.tmpl

where setup.py is a python script.  Previously, I was using an install script to symlink the script to my user's private bin (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 $ pip install .  from the project's root directory, and I am able to execute most of the script until I generate a template with jinja2.  It seems as though either the templates dir is not getting installed with the rest of the package, or my script is not finding the path to the templates dir correctly. 
Excerpt from foo.py:
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

Note: 'bar.tmpl' is passed to this function as template_file
Excerpt from setup.py:
from setuptools import setup

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

Contents of MANIFEST.in:
include templates/*

Relevant Traceback:
  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

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. 

A:

MANIFEST.in tell what files to include in the source distribution, i.e. python setup.py sdist, but it does not directly affect what files are installed because pip install .  just calls into setuptools and doesn't do anything special with package_data. 
You need to include the files in the setup.py file, either as package data or as additional files. 
See https://docs.python.org/2/distutils/setupscript.html for details, pay attention to data_files and package_data on the page.