packaging.rst
This file
Path
docs/tutorial/packaging.rst
108 lines · 3.9 KiB
Last changed by
Add Support for FLASK_ENV (#2570)Armin Ronacherbefore adoption2433522
History of this fileTake it
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 involves13creating 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 these16changes, your code structure should be::1718 /flaskr19 /flaskr20 __init__.py21 /static22 /templates23 flaskr.py24 schema.sql25 setup.py26 MANIFEST.in2728Create the ``setup.py`` file for ``flaskr`` with the following content::2930 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,44as well as the schema.4546Create the :file:`MANIFEST.in` and add the following lines::4748 graft flaskr/templates49 graft flaskr/static50 include flaskr/schema.sql5152Next, to simplify locating the application, create the file,53:file:`flaskr/__init__.py` containing only the following import statement::5455 from .flaskr import app5657This import statement brings the application instance into the top-level58of 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 be62``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`_.66With that said, from the ``flaskr/`` directory, go ahead and install the67application with::6869 pip install --editable .7071The above installation command assumes that it is run within the projects72root 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 output75of ``pip freeze``).7677With that out of the way, you should be able to start up the application.78Do this on Mac or Linux with the following commands in ``flaskr/``::7980 export FLASK_APP=flaskr81 export FLASK_ENV=development82 flask run8384(In case you are on Windows you need to use ``set`` instead of ``export``).85The :envvar:`FLASK_ENV` flag if set to ``development`` turns on all86development features such as enabling the interactive debugger.8788*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 in a browser.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