Skip to content
Myr

packaging.rst

This file

Path

docs/tutorial/packaging.rst

108 lines · 3.7 KiB

Last changed by

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

History of this file

Take it

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 1–108why, 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 app12following the `Python Packaging Guide`_. Presently this involves 13creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the 14projects root directory. You also need to add an :file:`__init__.py` 15file to make the :file:`flaskr/flaskr` directory a package. After these 16changes, your code structure should be::1718 /flaskr19 /flaskr20 __init__.py21 /static22 /templates23 flaskr.py24 schema.sql25 setup.py26 MANIFEST.in2728The content of the ``setup.py`` file for ``flaskr`` is:2930.. sourcecode:: python3132 from setuptools import setup3334 setup(35 name='flaskr',36 packages=['flaskr'],37 include_package_data=True,38 install_requires=[39 'flask',40 ],41 )4243When using setuptools, it is also necessary to specify any special files44that should be included in your package (in the :file:`MANIFEST.in`).45In this case, the static and templates directories need to be included,46as well as the schema. Create the :file:`MANIFEST.in` and add the47following lines::4849 graft flaskr/templates50 graft flaskr/static51 include flaskr/schema.sql5253To simplify locating the application, add the following import statement 54into this file, :file:`flaskr/__init__.py`:5556.. sourcecode:: python5758 from flaskr import app5960This import statement brings the application instance into the top-level 61of the application package. When it is time to run the application, the 62Flask development server needs the location of the app instance. This 63import statement simplifies the location process. Without it the export 64statement a few steps below would need to be 65``export FLASK_APP=flaskr.flaskr``.6667At this point you should be able to install the application. As usual, it68is recommended to install your Flask application within a `virtualenv`_.69With that said, go ahead and install the application with::7071 pip install --editable .7273The above installation command assumes that it is run within the projects 74root directory, `flaskr/`. The `editable` flag allows editing 75source code without having to reinstall the Flask app each time you make 76changes. The flaskr app is now installed in your virtualenv (see output 77of ``pip freeze``).7879With that out of the way, you should be able to start up the application.80Do this with the following commands::8182 export FLASK_APP=flaskr83 export FLASK_DEBUG=true84 flask run8586(In case you are on Windows you need to use `set` instead of `export`).87The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.88*Never leave debug mode activated in a production system*, because it will89allow users to execute code on the server!9091You will see a message telling you that server has started along with92the address at which you can access it.9394When you head over to the server in your browser, you will get a 404 error95because we don't have any views yet. That will be addressed a little later,96but first, you should get the database working.9798.. admonition:: Externally Visible Server99100 Want your server to be publicly available? Check out the101 :ref:`externally visible server <public-server>` section for more102 information.103104Continue with :ref:`tutorial-dbcon`.105106.. _Click: http://click.pocoo.org107.. _Python Packaging Guide: https://packaging.python.org108.. _virtualenv: https://virtualenv.pypa.io