Make the Project Installable

Making your project installable means that you can build adistribution file and install that in another environment, just likeyou installed Flask in your project’s environment. This makes deployingyour project the same as installing any other library, so you’re usingall the standard Python tools to manage everything.

Installing also comes with other benefits that might not be obvious fromthe tutorial or as a new Python user, including:

  • Currently, Python and Flask understand how to use the flaskrpackage only because you’re running from your project’s directory.Installing means you can import it no matter where you run from.

  • You can manage your project’s dependencies just like other packagesdo, so pip install yourproject.whl installs them.

  • Test tools can isolate your test environment from your developmentenvironment.

Note

This is being introduced late in the tutorial, but in your futureprojects you should always start with this.

Describe the Project

The setup.py file describes your project and the files that belongto it.

setup.py

  1. from setuptools import find_packages, setup
  2.  
  3. setup(
  4. name='flaskr',
  5. version='1.0.0',
  6. packages=find_packages(),
  7. include_package_data=True,
  8. zip_safe=False,
  9. install_requires=[
  10. 'flask',
  11. ],
  12. )

packages tells Python what package directories (and the Python filesthey contain) to include. find_packages() finds these directoriesautomatically so you don’t have to type them out. To include otherfiles, such as the static and templates directories,include_package_data is set. Python needs another file namedMANIFEST.in to tell what this other data is.

MANIFEST.in

  1. include flaskr/schema.sql
  2. graft flaskr/static
  3. graft flaskr/templates
  4. global-exclude *.pyc

This tells Python to copy everything in the static and templatesdirectories, and the schema.sql file, but to exclude all bytecodefiles.

See the official packaging guide for another explanation of the filesand options used.

Install the Project

Use pip to install your project in the virtual environment.

  1. $ pip install -e .

This tells pip to find setup.py in the current directory and installit in editable or development mode. Editable mode means that as youmake changes to your local code, you’ll only need to re-install if youchange the metadata about the project, such as its dependencies.

You can observe that the project is now installed with pip list.

  1. $ pip list
  2.  
  3. Package Version Location
  4. -------------- --------- ----------------------------------
  5. click 6.7
  6. Flask 1.0
  7. flaskr 1.0.0 /home/user/Projects/flask-tutorial
  8. itsdangerous 0.24
  9. Jinja2 2.10
  10. MarkupSafe 1.0
  11. pip 9.0.3
  12. setuptools 39.0.1
  13. Werkzeug 0.14.1
  14. wheel 0.30.0

Nothing changes from how you’ve been running your project so far.FLASK_APP is still set to flaskr and flask run still runsthe application, but you can call it from anywhere, not just theflask-tutorial directory.

Continue to Test Coverage.