jinja2.exceptions.UndefinedError: 'function' is undefined
Question: I am running a flask server on nginx + uwsgi. When I run just the flask server
via `python server.py`, I am able to use `id_encode` function in my jinja2
templates, no errors thrown.

However, when I launch (server.py) via

`uwsgi --socket 0.0.0.0:8002 --module server --callab app`

It will crash saying that it was unable to find the function `id_encode`.

`jinja2.exceptions.UndefinedError: 'id_encode' is undefined`

Declaring via:

    
    
    if __name__ == '__main__':
        app.jinja_env.globals.update(id_encode=id_encode)
        app.run(host=host,port=5000, debug=True)
    

What's causing this problem and how can I make the function available?


Answer: The issue is that the `__main__` block will only get executed if the script is
_run_ as a top level script. uwsgi _imports_ your module and so the `__main__`
block is never run. Move your
`app.jinja_env.globals.update(id_encode=id_encode)` outside of the `__main__`
block and everything should work correctly.

