Skip to content
Myr

packages.rst

This file

Path

docs/patterns/packages.rst

140 lines · 4.3 KiB

Last changed by

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption12008c2

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:file:`yourapplication` inside the existing one and move everything below it.25Then rename :file:`yourapplication.py` to :file:`__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 :file:`setup.py` next to the inner45:file:`yourapplication` folder with the following contents::4647    from setuptools import setup4849    setup(50        name='yourapplication',51        packages=['yourapplication'],52        include_package_data=True,53        install_requires=[54            'flask',55        ],56    )5758In order to run the application you need to export an environment variable 59that tells Flask where to find the application instance:: 6061    export FLASK_APP=yourapplication6263If you are outside of the project directory make sure to provide the exact 64path to your application directory. Similiarly you can turn on "debug 65mode" with this environment variable:: 6667    export FLASK_DEBUG=true 6869In order to install and run the application you need to issue the following 70commands::7172    pip install -e . 73    flask run7475What did we gain from this?  Now we can restructure the application a bit76into multiple modules.  The only thing you have to remember is the77following quick checklist:78791. the `Flask` application object creation has to be in the80   :file:`__init__.py` file.  That way each module can import it safely and the81   `__name__` variable will resolve to the correct package.822. all the view functions (the ones with a :meth:`~flask.Flask.route`83   decorator on top) have to be imported in the :file:`__init__.py` file.84   Not the object itself, but the module it is in. Import the view module85   **after the application object is created**.8687Here's an example :file:`__init__.py`::8889    from flask import Flask90    app = Flask(__name__)9192    import yourapplication.views9394And this is what :file:`views.py` would look like::9596    from yourapplication import app9798    @app.route('/')99    def index():100        return 'Hello World!'101102You should then end up with something like that::103104    /yourapplication105        setup.py106        /yourapplication107            __init__.py108            views.py109            /static110                style.css111            /templates112                layout.html113                index.html114                login.html115                ...116117.. admonition:: Circular Imports118119   Every Python programmer hates them, and yet we just added some:120   circular imports (That's when two modules depend on each other.  In this121   case :file:`views.py` depends on :file:`__init__.py`).  Be advised that this is a122   bad idea in general but here it is actually fine.  The reason for this is123   that we are not actually using the views in :file:`__init__.py` and just124   ensuring the module is imported and we are doing that at the bottom of125   the file.126127   There are still some problems with that approach but if you want to use128   decorators there is no way around that.  Check out the129   :ref:`becomingbig` section for some inspiration how to deal with that.130131132.. _working-with-modules:133134Working with Blueprints135-----------------------136137If you have larger applications it's recommended to divide them into138smaller groups where each group is implemented with the help of a139blueprint.  For a gentle introduction into this topic refer to the140:ref:`blueprints` chapter of the documentation.