Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

114 lines · 3.6 KiB

Last changed by

Removed old module documentation and added some new for blueprints.Armin Ronacherbefore adoptiondbfa04e

History of this file

Take it

1.. _larger-applications:23Larger Applications4===================56For larger applications it's a good idea to use a package instead of a7module.  That is quite simple.  Imagine a small application looks like8this::910    /yourapplication11        /yourapplication.py12        /static13            /style.css14        /templates15            layout.html16            index.html17            login.html18            ...1920Simple Packages21---------------2223To convert that into a larger one, just create a new folder24`yourapplication` inside the existing one and move everything below it.25Then rename `yourapplication.py` to `__init__.py`.  (Make sure to delete26all `.pyc` files first, otherwise things would most likely break)2728You should then end up with something like that::2930    /yourapplication31        /yourapplication32            /__init__.py33            /static34                /style.css35            /templates36                layout.html37                index.html38                login.html39                ...4041But how do you run your application now?  The naive ``python42yourapplication/__init__.py`` will not work.  Let's just say that Python43does not want modules in packages to be the startup file.  But that is not44a big problem, just add a new file called `runserver.py` next to the inner45`yourapplication` folder with the following contents::4647    from yourapplication import app48    app.run(debug=True)4950What did we gain from this?  Now we can restructure the application a bit51into multiple modules.  The only thing you have to remember is the52following quick checklist:53541. the `Flask` application object creation has to be in the55   `__init__.py` file.  That way each module can import it safely and the56   `__name__` variable will resolve to the correct package.572. all the view functions (the ones with a :meth:`~flask.Flask.route`58   decorator on top) have to be imported when in the `__init__.py` file.59   Not the object itself, but the module it is in. Import the view module60   **after the application object is created**.6162Here's an example `__init__.py`::6364    from flask import Flask65    app = Flask(__name__)6667    import yourapplication.views6869And this is what `views.py` would look like::7071    from yourapplication import app7273    @app.route('/')74    def index():75        return 'Hello World!'7677You should then end up with something like that::7879    /yourapplication80        /yourapplication81            /__init__.py82            /views.py83            /static84                /style.css85            /templates86                layout.html87                index.html88                login.html89                ...9091.. admonition:: Circular Imports9293   Every Python programmer hates them, and yet we just added some:94   circular imports (That's when two modules depend on each other.  In this95   case `views.py` depends on `__init__.py`).  Be advised that this is a96   bad idea in general but here it is actually fine.  The reason for this is97   that we are not actually using the views in `__init__.py` and just98   ensuring the module is imported and we are doing that at the bottom of99   the file.100101   There are still some problems with that approach but if you want to use102   decorators there is no way around that.  Check out the103   :ref:`becomingbig` section for some inspiration how to deal with that.104105106.. _working-with-modules:107108Working with Blueprints109-----------------------110111If you have larger applications it's recommended to divide them into112smaller groups where each group is implemented with the help of a113blueprint.  For a gentle introduction into this topic refer to the114:ref:`blueprints` chapter of the documentation.