need to package jinja2 template for python
Question: (UPDATE: I've made a better question with a better answer
[here](http://stackoverflow.com/questions/38642557/how-to-load-jinja-template-
directly-from-filesystem). I was going to delete this question, but some of
the answers might prove useful to future searchers.)

My question is just about identical to
[this](http://stackoverflow.com/questions/29150156/how-to-make-a-python-
package-containing-only-jinja-templates), but that answer is ugly (requires a
dir structure including `sharedtemplates/templates/templates/`), incomplete as
posted (user "answered" his own question), and assumes some knowledge I don't
have.

I'm working on my first python-backed web application. The javascript
component is well under development using a static HTML page. Now I want a
server-side python component to handle AJAX calls and render an HTML template
using jinja2.

I've had success with python before, creating GUI apps using tkinter/requests.
Love the language, but the python environment (environments?) is confusing.
I'm not working in a `virtualenv`.

According to [jinja2 docs](http://jinja.pocoo.org/docs/dev/api/#high-level-
api), HTML templates have to be in something called a package. Then you create
an `Environment` with a `PackageLoader` that knows the name of the package and
the template dir:

    
    
    from jinja2 import Environment, PackageLoader
    env = Environment(loader=PackageLoader('yourapplication', 'templates'))
    

So, here's my index.py (it's just a stub and doesn't even try to render
anything, but you can at least tell if it crashes).

    
    
    #!/usr/bin/python
    from jinja2 import Environment, PackageLoader # no prob, jinja2 correctly installed using pip
    
    env = Environment(loader=PackageLoader('mypkg', 'template')) # causes server error
    
    # if it doesn't crash, just put up a basic html page for now
    print ("Content-type: text/html\r\n\r\n")
    print("<html><head><title>hello</title></head><body>hello wuld</body></html>")
    

Here's the directory structure:

    
    
    index.py
    mypkg/
    mypkg/template/index.html
    mypkg/__init__.py  # empty
    

Relevant line from error log:

    
    
    ImportError: No module named mypkg
    

Maybe I need to structure this differently, and I'm pretty sure I'll need to
create and invoke a `setup.py` to install the module. That's part of what the
other answer left out: what's in `setup.py` and how does it work in this case?
I've looked at dozens of resources on `setup.py` and none of them seems to
pertain to the question of installing HTML templates.

Thanks in advance for your help!

UPDATE: fragilewindows pointed to a resource that tells about "developer
mode", which is probably part of the answer. The difficulty here is, I'm
looking to package this template for local deployment, not for distribution.
But 99% of the online documentation is about packaging projects for PyPi. I
don't need to package a "project", just a dinky HTML template. Indeed, the
only reason I need to package the template is because that's the default way
for `jinja2` to access templates (and I do want to go native in python).

I just need to convince the environment that "mypkg" is installed, and that
"template" is a directory within the install. You can see that my efforts so
far are naive; I expect the right answer will be correspondingly lightweight.


Answer: I don't know the process involved with packaging but I figure since Jinja2 is
written in Python, the process would be the same as packaging any other
application in Python.

Here are a few links that may be useful to you:

  * The Hitchhiker's Guide to Python (great resource): [Packaging Explained](http://docs.python-guide.org/en/latest/shipping/packaging/)
  * Alternatives to Packaging: [freeze your application](http://docs.python-guide.org/en/latest/shipping/freezing/#freezing-your-code-ref)
  * [Python Packaging User Guide](https://packaging.python.org/distributing/) (probably the most useful to you)

I hope this helps.

