Advanced tutorial: How to write reusable apps

This advanced tutorial begins where Tutorial 7left off. We’ll be turning our Web-poll into a standalone Python packageyou can reuse in new projects and share with other people.

If you haven’t recently completed Tutorials 1–7, we encourage you to reviewthese so that your example project matches the one described below.

Reusability matters

It’s a lot of work to design, build, test and maintain a web application. ManyPython and Django projects share common problems. Wouldn’t it be great if wecould save some of this repeated work?

Reusability is the way of life in Python. The Python Package Index (PyPI) has a vast range of packages you can use in your ownPython programs. Check out Django Packages forexisting reusable apps you could incorporate in your project. Django itself isalso a normal Python package. This means that you can take existing Pythonpackages or Django apps and compose them into your own web project. You onlyneed to write the parts that make your project unique.

Let’s say you were starting a new project that needed a polls app like the onewe’ve been working on. How do you make this app reusable? Luckily, you’re wellon the way already. In Tutorial 1, we saw how wecould decouple polls from the project-level URLconf using an include.In this tutorial, we’ll take further steps to make the app easy to use in newprojects and ready to publish for others to install and use.

Package? App?

A Python package provides a way of grouping related Python code foreasy reuse. A package contains one or more files of Python code (also knownas “modules”).

A package can be imported with import foo.bar or from foo importbar. For a directory (like polls) to form a package, it must containa special file init.py, even if this file is empty.

A Django application is a Python package that is specifically intendedfor use in a Django project. An application may use common Djangoconventions, such as having models, tests, urls, and viewssubmodules.

Later on we use the term packaging to describe the process of making aPython package easy for others to install. It can be a little confusing, weknow.

Your project and your reusable app

After the previous tutorials, our project should look like this:

  1. mysite/
  2. manage.py
  3. mysite/
  4. __init__.py
  5. settings.py
  6. urls.py
  7. wsgi.py
  8. polls/
  9. __init__.py
  10. admin.py
  11. migrations/
  12. __init__.py
  13. 0001_initial.py
  14. models.py
  15. static/
  16. polls/
  17. images/
  18. background.gif
  19. style.css
  20. templates/
  21. polls/
  22. detail.html
  23. index.html
  24. results.html
  25. tests.py
  26. urls.py
  27. views.py
  28. templates/
  29. admin/
  30. base_site.html

You created mysite/templates in Tutorial 7,and polls/templates in Tutorial 3. Now perhapsit is clearer why we chose to have separate template directories for theproject and application: everything that is part of the polls application is inpolls. It makes the application self-contained and easier to drop into anew project.

The polls directory could now be copied into a new Django project andimmediately reused. It’s not quite ready to be published though. For that, weneed to package the app to make it easy for others to install.

Installing some prerequisites

The current state of Python packaging is a bit muddled with various tools. Forthis tutorial, we’re going to use setuptools to build our package. It’s therecommended packaging tool (merged with the distribute fork). We’ll also beusing pip to install and uninstall it. You should install thesetwo packages now. If you need help, you can refer to how to installDjango with pip. You can install setuptoolsthe same way.

Packaging your app

Python packaging refers to preparing your app in a specific format that canbe easily installed and used. Django itself is packaged very much likethis. For a small app like polls, this process isn’t too difficult.

  • First, create a parent directory for polls, outside of your Djangoproject. Call this directory django-polls.

Choosing a name for your app

When choosing a name for your package, check resources like PyPI to avoidnaming conflicts with existing packages. It’s often useful to prependdjango- to your module name when creating a package to distribute.This helps others looking for Django apps identify your app as Djangospecific.

Application labels (that is, the final part of the dotted path toapplication packages) must be unique in INSTALLED_APPS.Avoid using the same label as any of the Django contrib packages, for example auth, admin, ormessages.

  • Move the polls directory into the django-polls directory.

  • Create a file django-polls/README.rst with the following contents:

django-polls/README.rst

  1. =====
  2. Polls
  3. =====
  4.  
  5. Polls is a Django app to conduct Web-based polls. For each question,
  6. visitors can choose between a fixed number of answers.
  7.  
  8. Detailed documentation is in the "docs" directory.
  9.  
  10. Quick start
  11. -----------
  12.  
  13. 1. Add "polls" to your INSTALLED_APPS setting like this::
  14.  
  15. INSTALLED_APPS = [
  16. ...
  17. 'polls',
  18. ]
  19.  
  20. 2. Include the polls URLconf in your project urls.py like this::
  21.  
  22. path('polls/', include('polls.urls')),
  23.  
  24. 3. Run `python manage.py migrate` to create the polls models.
  25.  
  26. 4. Start the development server and visit http://127.0.0.1:8000/admin/
  27. to create a poll (you'll need the Admin app enabled).
  28.  
  29. 5. Visit http://127.0.0.1:8000/polls/ to participate in the poll.
  • Create a django-polls/LICENSE file. Choosing a license is beyond thescope of this tutorial, but suffice it to say that code released publiclywithout a license is useless. Django and many Django-compatible apps aredistributed under the BSD license; however, you’re free to pick your ownlicense. Just be aware that your licensing choice will affect who is ableto use your code.

  • Next we’ll create a setup.py file which provides details about how tobuild and install the app. A full explanation of this file is beyond thescope of this tutorial, but the setuptools docs have a goodexplanation. Create a file django-polls/setup.py with the followingcontents:

django-polls/setup.py

  1. import os
  2. from setuptools import find_packages, setup
  3.  
  4. with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
  5. README = readme.read()
  6.  
  7. # allow setup.py to be run from any path
  8. os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
  9.  
  10. setup(
  11. name='django-polls',
  12. version='0.1',
  13. packages=find_packages(),
  14. include_package_data=True,
  15. license='BSD License', # example license
  16. description='A Django app to conduct Web-based polls.',
  17. long_description=README,
  18. url='https://www.example.com/',
  19. author='Your Name',
  20. author_email='yourname@example.com',
  21. classifiers=[
  22. 'Environment :: Web Environment',
  23. 'Framework :: Django',
  24. 'Framework :: Django :: X.Y', # replace "X.Y" as appropriate
  25. 'Intended Audience :: Developers',
  26. 'License :: OSI Approved :: BSD License', # example license
  27. 'Operating System :: OS Independent',
  28. 'Programming Language :: Python',
  29. 'Programming Language :: Python :: 3.6',
  30. 'Programming Language :: Python :: 3.7',
  31. 'Topic :: Internet :: WWW/HTTP',
  32. 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
  33. ],
  34. )
  • Only Python modules and packages are included in the package by default. Toinclude additional files, we’ll need to create a MANIFEST.in file. Thesetuptools docs referred to in the previous step discuss this file in moredetails. To include the templates, the README.rst and our LICENSEfile, create a file django-polls/MANIFEST.in with the followingcontents:

django-polls/MANIFEST.in

  1. include LICENSE
  2. include README.rst
  3. recursive-include polls/static *
  4. recursive-include polls/templates *
  • It’s optional, but recommended, to include detailed documentation with yourapp. Create an empty directory django-polls/docs for futuredocumentation. Add an additional line to django-polls/MANIFEST.in:
  1. recursive-include docs *

Note that the docs directory won’t be included in your package unlessyou add some files to it. Many Django apps also provide their documentationonline through sites like readthedocs.org.

  • Try building your package with python setup.py sdist (run from insidedjango-polls). This creates a directory called dist and builds yournew package, django-polls-0.1.tar.gz.

For more information on packaging, see Python’s Tutorial on Packaging andDistributing Projects.

Using your own package

Since we moved the polls directory out of the project, it’s no longerworking. We’ll now fix this by installing our new django-polls package.

Installing as a user library

The following steps install django-polls as a user library. Per-userinstalls have a lot of advantages over installing the package system-wide,such as being usable on systems where you don’t have administrator accessas well as preventing the package from affecting system services and otherusers of the machine.

Note that per-user installations can still affect the behavior of systemtools that run as that user, so virtualenv is a more robust solution(see below).

  • To install the package, use pip (you already installed it, right?):
  1. python -m pip install --user django-polls/dist/django-polls-0.1.tar.gz
  • With luck, your Django project should now work correctly again. Run theserver again to confirm this.

  • To uninstall the package, use pip:

  1. python -m pip uninstall django-polls

Publishing your app

Now that we’ve packaged and tested django-polls, it’s ready to share withthe world! If this wasn’t just an example, you could now:

Installing Python packages with virtualenv

Earlier, we installed the polls app as a user library. This has somedisadvantages:

  • Modifying the user libraries can affect other Python software on your system.
  • You won’t be able to run multiple versions of this package (or others withthe same name).Typically, these situations only arise once you’re maintaining several Djangoprojects. When they do, the best solution is to use virtualenv. This tool allows you to maintain multipleisolated Python environments, each with its own copy of the libraries andpackage namespace.