Skip to content
Myr

packaging.rst

This file

Path

docs/tutorial/packaging.rst

106 lines · 3.8 KiB

Last changed by

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

History of this file

Take it

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 1–11why, one step back
1.. _tutorial-packaging:
23Step 3: Installing flaskr as a Package4======================================56Flask is now shipped with built-in support for `Click`_. Click provides7Flask with enhanced and extensible command line utilities. Later in this8tutorial you will see exactly how to extend the ``flask`` command line9interface (CLI).1011A useful pattern to manage a Flask application is to install your app

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 12–15why, one step back
12following the `Python Packaging Guide`_. Presently this involves
13creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the14projects root directory. You also need to add an :file:`__init__.py`15file to make the :file:`flaskr/flaskr` directory a package. After these

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 16–27why, one step back
16changes, your code structure should be::
1718 /flaskr19 /flaskr20 __init__.py21 /static22 /templates23 flaskr.py24 schema.sql25 setup.py26 MANIFEST.in27

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 28why, one step back
28Create the ``setup.py`` file for ``flaskr`` with the following content::

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 29–43why, one step back
29
30 from setuptools import setup3132 setup(33 name='flaskr',34 packages=['flaskr'],35 include_package_data=True,36 install_requires=[37 'flask',38 ],39 )4041When using setuptools, it is also necessary to specify any special files42that should be included in your package (in the :file:`MANIFEST.in`).43In this case, the static and templates directories need to be included,

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 44why, one step back
44as well as the schema.

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 45why, one step back
45

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 46–47why, one step back
46Create the :file:`MANIFEST.in` and add the following lines::
47

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 48–51why, one step back
48 graft flaskr/templates
49 graft flaskr/static50 include flaskr/schema.sql51

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 52–53why, one step back
52Next, to simplify locating the application, create the file,
53:file:`flaskr/__init__.py` containing only the following import statement::

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 54why, one step back
54

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption2b8fef4

line 55why, one step back
55 from .flaskr import app

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 56why, one step back
56

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 57–61why, one step back
57This import statement brings the application instance into the top-level
58of the application package. When it is time to run the application, the59Flask development server needs the location of the app instance. This60import statement simplifies the location process. Without the above61import statement, the export statement a few steps below would need to be

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 62–65why, one step back
62``export FLASK_APP=flaskr.flaskr``.
6364At this point you should be able to install the application. As usual, it65is recommended to install your Flask application within a `virtualenv`_.

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 66–67why, one step back
66With that said, from the ``flaskr/`` directory, go ahead and install the
67application with::

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 68–70why, one step back
68
69 pip install --editable .70

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

lines 71–74why, one step back
71The above installation command assumes that it is run within the projects
72root directory, ``flaskr/``. The ``editable`` flag allows editing73source code without having to reinstall the Flask app each time you make74changes. The flaskr app is now installed in your virtualenv (see output

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 75–77why, one step back
75of ``pip freeze``).
7677With that out of the way, you should be able to start up the application.

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 78why, one step back
78Do this on Mac or Linux with the following commands in ``flaskr/``::

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 79–83why, one step back
79
80 export FLASK_APP=flaskr81 export FLASK_DEBUG=true82 flask run83

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 84why, one step back
84(In case you are on Windows you need to use ``set`` instead of ``export``).

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 85–89why, one step back
85The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.
86*Never leave debug mode activated in a production system*, because it will87allow users to execute code on the server!8889You will see a message telling you that server has started along with

Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89

line 90why, one step back
90the address at which you can access it in a browser.

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 91–106why, one step back
91
92When you head over to the server in your browser, you will get a 404 error93because we don't have any views yet. That will be addressed a little later,94but first, you should get the database working.9596.. admonition:: Externally Visible Server9798 Want your server to be publicly available? Check out the99 :ref:`externally visible server <public-server>` section for more100 information.101102Continue with :ref:`tutorial-dbcon`.103104.. _Click: http://click.pocoo.org105.. _Python Packaging Guide: https://packaging.python.org106.. _virtualenv: https://virtualenv.pypa.io