{"title":"Advanced tutorial: How to write reusable apps","version":"1.11","locale":"en","docname":"intro/reusable-apps","url":"/en/1.11/intro/reusable-apps/","canonical":"https://djangodocs.dev/en/1.11/intro/reusable-apps/","summary":"This advanced tutorial begins where Tutorial 7 left off. We’ll be turning our Web-poll into a standalone Python package you can reuse in new projects and share with…","html":"<h1>Advanced tutorial: How to write reusable apps<a class=\"heading-anchor\" href=\"#advanced-tutorial-how-to-write-reusable-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This advanced tutorial begins where <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial07/\"><span class=\"doc\">Tutorial 7</span></a>\nleft off. We’ll be turning our Web-poll into a standalone Python package\nyou can reuse in new projects and share with other people.</p>\n<p>If you haven’t recently completed Tutorials 1–7, we encourage you to review\nthese so that your example project matches the one described below.</p>\n<section id=\"reusability-matters\">\n<h2>Reusability matters<a class=\"heading-anchor\" href=\"#reusability-matters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>It’s a lot of work to design, build, test and maintain a web application. Many\nPython and Django projects share common problems. Wouldn’t it be great if we\ncould save some of this repeated work?</p>\n<p>Reusability is the way of life in Python. <a class=\"reference external\" href=\"https://pypi.python.org/pypi\">The Python Package Index (PyPI)</a> has a vast range of packages you can use in\nyour own Python programs. Check out <a class=\"reference external\" href=\"https://djangopackages.org\">Django Packages</a> for existing reusable apps you could incorporate\nin your project. Django itself is also just a Python package. This means that\nyou can take existing Python packages or Django apps and compose them into your\nown web project. You only need to write the parts that make your project unique.</p>\n<p>Let’s say you were starting a new project that needed a polls app like the one\nwe’ve been working on. How do you make this app reusable? Luckily, you’re well\non the way already. In <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial03/\"><span class=\"doc\">Tutorial 3</span></a>, we saw how we\ncould decouple polls from the project-level URLconf using an <code class=\"docutils literal notranslate\"><span class=\"pre\">include</span></code>.\nIn this tutorial, we’ll take further steps to make the app easy to use in new\nprojects and ready to publish for others to install and use.</p>\n<aside class=\"admonition-package-app admonition\">\n<p class=\"admonition-title\">Package? App?</p>\n<p>A Python <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-package\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">package</span></a> provides a way of grouping related Python code for\neasy reuse. A package contains one or more files of Python code (also known\nas “modules”).</p>\n<p>A package can be imported with <code class=\"docutils literal notranslate\"><span class=\"pre\">import</span> <span class=\"pre\">foo.bar</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">from</span> <span class=\"pre\">foo</span> <span class=\"pre\">import</span>\n<span class=\"pre\">bar</span></code>. For a directory (like <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>) to form a package, it must contain\na special file <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__.py</span></code>, even if this file is empty.</p>\n<p>A Django <em>application</em> is just a Python package that is specifically\nintended for use in a Django project. An application may use common Django\nconventions, such as having <code class=\"docutils literal notranslate\"><span class=\"pre\">models</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">tests</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">urls</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">views</span></code>\nsubmodules.</p>\n<p>Later on we use the term <em>packaging</em> to describe the process of making a\nPython package easy for others to install. It can be a little confusing, we\nknow.</p>\n</aside>\n</section>\n<section id=\"your-project-and-your-reusable-app\">\n<h2>Your project and your reusable app<a class=\"heading-anchor\" href=\"#your-project-and-your-reusable-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>After the previous tutorials, our project should look like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">mysite</span><span class=\"o\">/</span>\n    <span class=\"n\">manage</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">mysite</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">urls</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">wsgi</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">polls</span><span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">migrations</span><span class=\"o\">/</span>\n            <span class=\"fm\">__init__</span><span class=\"o\">.</span><span class=\"n\">py</span>\n            <span class=\"mi\">0001</span><span class=\"n\">_initial</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">static</span><span class=\"o\">/</span>\n            <span class=\"n\">polls</span><span class=\"o\">/</span>\n                <span class=\"n\">images</span><span class=\"o\">/</span>\n                    <span class=\"n\">background</span><span class=\"o\">.</span><span class=\"n\">gif</span>\n                <span class=\"n\">style</span><span class=\"o\">.</span><span class=\"n\">css</span>\n        <span class=\"n\">templates</span><span class=\"o\">/</span>\n            <span class=\"n\">polls</span><span class=\"o\">/</span>\n                <span class=\"n\">detail</span><span class=\"o\">.</span><span class=\"n\">html</span>\n                <span class=\"n\">index</span><span class=\"o\">.</span><span class=\"n\">html</span>\n                <span class=\"n\">results</span><span class=\"o\">.</span><span class=\"n\">html</span>\n        <span class=\"n\">tests</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">urls</span><span class=\"o\">.</span><span class=\"n\">py</span>\n        <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">py</span>\n    <span class=\"n\">templates</span><span class=\"o\">/</span>\n        <span class=\"n\">admin</span><span class=\"o\">/</span>\n            <span class=\"n\">base_site</span><span class=\"o\">.</span><span class=\"n\">html</span>\n</code></pre></div>\n<p>You created <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite/templates</span></code> in <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial07/\"><span class=\"doc\">Tutorial 7</span></a>,\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates</span></code> in <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial03/\"><span class=\"doc\">Tutorial 3</span></a>. Now perhaps\nit is clearer why we chose to have separate template directories for the\nproject and application: everything that is part of the polls application is in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>. It makes the application self-contained and easier to drop into a\nnew project.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> directory could now be copied into a new Django project and\nimmediately reused. It’s not quite ready to be published though. For that, we\nneed to package the app to make it easy for others to install.</p>\n</section>\n<section id=\"installing-some-prerequisites\">\n<span id=\"installing-reusable-apps-prerequisites\"></span><h2>Installing some prerequisites<a class=\"heading-anchor\" href=\"#installing-some-prerequisites\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The current state of Python packaging is a bit muddled with various tools. For\nthis tutorial, we’re going to use <a class=\"reference external\" href=\"https://pypi.python.org/pypi/setuptools\">setuptools</a> to build our package. It’s the\nrecommended packaging tool (merged with the <code class=\"docutils literal notranslate\"><span class=\"pre\">distribute</span></code> fork). We’ll also be\nusing <a class=\"reference external\" href=\"https://pypi.python.org/pypi/pip\">pip</a> to install and uninstall it. You should install these\ntwo packages now. If you need help, you can refer to <a class=\"reference internal\" href=\"/en/1.11/topics/install/#installing-official-release\"><span class=\"std std-ref\">how to install\nDjango with pip</span></a>. You can install <code class=\"docutils literal notranslate\"><span class=\"pre\">setuptools</span></code>\nthe same way.</p>\n</section>\n<section id=\"packaging-your-app\">\n<h2>Packaging your app<a class=\"heading-anchor\" href=\"#packaging-your-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Python <em>packaging</em> refers to preparing your app in a specific format that can\nbe easily installed and used. Django itself is packaged very much like\nthis. For a small app like polls, this process isn’t too difficult.</p>\n<ol class=\"arabic\">\n<li><p>First, create a parent directory for <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>, outside of your Django\nproject. Call this directory <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code>.</p>\n<aside class=\"admonition-choosing-a-name-for-your-app admonition\">\n<p class=\"admonition-title\">Choosing a name for your app</p>\n<p>When choosing a name for your package, check resources like PyPI to avoid\nnaming conflicts with existing packages. It’s often useful to prepend\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django-</span></code> to your module name when creating a package to distribute.\nThis helps others looking for Django apps identify your app as Django\nspecific.</p>\n<p>Application labels (that is, the final part of the dotted path to\napplication packages) <em>must</em> be unique in <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>.\nAvoid using the same label as any of the Django <a class=\"reference internal\" href=\"/en/1.11/ref/contrib/\"><span class=\"doc\">contrib packages</span></a>, for example <code class=\"docutils literal notranslate\"><span class=\"pre\">auth</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">admin</span></code>, or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">messages</span></code>.</p>\n</aside>\n</li>\n<li><p>Move the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> directory into the <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code> directory.</p></li>\n<li><p>Create a file <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/README.rst</span></code> with the following contents:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>django-polls/README.rst</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"django-polls/README.rst\"><code>=====\nPolls\n=====\n\nPolls is a simple Django app to conduct Web-based polls. For each\nquestion, visitors can choose between a fixed number of answers.\n\nDetailed documentation is in the &quot;docs&quot; directory.\n\nQuick start\n-----------\n\n1. Add &quot;polls&quot; to your INSTALLED_APPS setting like this::\n\n    INSTALLED_APPS = [\n        ...\n        &#39;polls&#39;,\n    ]\n\n2. Include the polls URLconf in your project urls.py like this::\n\n    url(r&#39;^polls/&#39;, include(&#39;polls.urls&#39;)),\n\n3. Run `python manage.py migrate` to create the polls models.\n\n4. Start the development server and visit http://127.0.0.1:8000/admin/\n   to create a poll (you&#39;ll need the Admin app enabled).\n\n5. Visit http://127.0.0.1:8000/polls/ to participate in the poll.\n</code></pre></figure>\n</li>\n<li><p>Create a <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/LICENSE</span></code> file. Choosing a license is beyond the\nscope of this tutorial, but suffice it to say that code released publicly\nwithout a license is <em>useless</em>. Django and many Django-compatible apps are\ndistributed under the BSD license; however, you’re free to pick your own\nlicense. Just be aware that your licensing choice will affect who is able\nto use your code.</p></li>\n<li><p>Next we’ll create a <code class=\"docutils literal notranslate\"><span class=\"pre\">setup.py</span></code> file which provides details about how to\nbuild and install the app.  A full explanation of this file is beyond the\nscope of this tutorial, but the <a class=\"reference external\" href=\"https://setuptools.readthedocs.io/en/latest/\">setuptools docs</a> have a good\nexplanation. Create a file <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/setup.py</span></code> with the following\ncontents:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>django-polls/setup.py</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"django-polls/setup.py\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">os</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">setuptools</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">find_packages</span><span class=\"p\">,</span> <span class=\"n\">setup</span>\n\n<span class=\"k\">with</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">dirname</span><span class=\"p\">(</span><span class=\"vm\">__file__</span><span class=\"p\">),</span> <span class=\"s1\">&#39;README.rst&#39;</span><span class=\"p\">))</span> <span class=\"k\">as</span> <span class=\"n\">readme</span><span class=\"p\">:</span>\n    <span class=\"n\">README</span> <span class=\"o\">=</span> <span class=\"n\">readme</span><span class=\"o\">.</span><span class=\"n\">read</span><span class=\"p\">()</span>\n\n<span class=\"c1\"># allow setup.py to be run from any path</span>\n<span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">chdir</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">normpath</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">abspath</span><span class=\"p\">(</span><span class=\"vm\">__file__</span><span class=\"p\">),</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">pardir</span><span class=\"p\">)))</span>\n\n<span class=\"n\">setup</span><span class=\"p\">(</span>\n    <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;django-polls&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">version</span><span class=\"o\">=</span><span class=\"s1\">&#39;0.1&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">packages</span><span class=\"o\">=</span><span class=\"n\">find_packages</span><span class=\"p\">(),</span>\n    <span class=\"n\">include_package_data</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n    <span class=\"n\">license</span><span class=\"o\">=</span><span class=\"s1\">&#39;BSD License&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># example license</span>\n    <span class=\"n\">description</span><span class=\"o\">=</span><span class=\"s1\">&#39;A simple Django app to conduct Web-based polls.&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">long_description</span><span class=\"o\">=</span><span class=\"n\">README</span><span class=\"p\">,</span>\n    <span class=\"n\">url</span><span class=\"o\">=</span><span class=\"s1\">&#39;https://www.example.com/&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">author</span><span class=\"o\">=</span><span class=\"s1\">&#39;Your Name&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">author_email</span><span class=\"o\">=</span><span class=\"s1\">&#39;yourname@example.com&#39;</span><span class=\"p\">,</span>\n    <span class=\"n\">classifiers</span><span class=\"o\">=</span><span class=\"p\">[</span>\n        <span class=\"s1\">&#39;Environment :: Web Environment&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Framework :: Django&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Framework :: Django :: X.Y&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># replace &quot;X.Y&quot; as appropriate</span>\n        <span class=\"s1\">&#39;Intended Audience :: Developers&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;License :: OSI Approved :: BSD License&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># example license</span>\n        <span class=\"s1\">&#39;Operating System :: OS Independent&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Programming Language :: Python&#39;</span><span class=\"p\">,</span>\n        <span class=\"c1\"># Replace these appropriately if you are stuck on Python 2.</span>\n        <span class=\"s1\">&#39;Programming Language :: Python :: 3&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Programming Language :: Python :: 3.4&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Programming Language :: Python :: 3.5&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Topic :: Internet :: WWW/HTTP&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Topic :: Internet :: WWW/HTTP :: Dynamic Content&#39;</span><span class=\"p\">,</span>\n    <span class=\"p\">],</span>\n<span class=\"p\">)</span>\n</code></pre></figure>\n</li>\n<li><p>Only Python modules and packages are included in the package by default. To\ninclude additional files, we’ll need to create a <code class=\"docutils literal notranslate\"><span class=\"pre\">MANIFEST.in</span></code> file. The\nsetuptools docs referred to in the previous step discuss this file in more\ndetails. To include the templates, the <code class=\"docutils literal notranslate\"><span class=\"pre\">README.rst</span></code> and our <code class=\"docutils literal notranslate\"><span class=\"pre\">LICENSE</span></code>\nfile, create a file <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/MANIFEST.in</span></code> with the following\ncontents:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>django-polls/MANIFEST.in</code></figcaption><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"django-polls/MANIFEST.in\"><code><span class=\"n\">include</span> <span class=\"n\">LICENSE</span>\n<span class=\"n\">include</span> <span class=\"n\">README</span><span class=\"o\">.</span><span class=\"n\">rst</span>\n<span class=\"n\">recursive</span><span class=\"o\">-</span><span class=\"n\">include</span> <span class=\"n\">polls</span><span class=\"o\">/</span><span class=\"n\">static</span> <span class=\"o\">*</span>\n<span class=\"n\">recursive</span><span class=\"o\">-</span><span class=\"n\">include</span> <span class=\"n\">polls</span><span class=\"o\">/</span><span class=\"n\">templates</span> <span class=\"o\">*</span>\n</code></pre></figure>\n</li>\n<li><p>It’s optional, but recommended, to include detailed documentation with your\napp. Create an empty directory <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/docs</span></code> for future\ndocumentation. Add an additional line to <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls/MANIFEST.in</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">recursive</span><span class=\"o\">-</span><span class=\"n\">include</span> <span class=\"n\">docs</span> <span class=\"o\">*</span>\n</code></pre></div>\n<p>Note that the <code class=\"docutils literal notranslate\"><span class=\"pre\">docs</span></code> directory won’t be included in your package unless\nyou add some files to it. Many Django apps also provide their documentation\nonline through sites like <a class=\"reference external\" href=\"https://readthedocs.org\">readthedocs.org</a>.</p>\n</li>\n<li><p>Try building your package with <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">setup.py</span> <span class=\"pre\">sdist</span></code> (run from inside\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code>). This creates a directory called <code class=\"docutils literal notranslate\"><span class=\"pre\">dist</span></code> and builds your\nnew package, <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls-0.1.tar.gz</span></code>.</p></li>\n</ol>\n<p>For more information on packaging, see Python’s <a class=\"reference external\" href=\"https://packaging.python.org/tutorials/packaging-projects/\">Tutorial on Packaging and\nDistributing Projects</a>.</p>\n</section>\n<section id=\"using-your-own-package\">\n<h2>Using your own package<a class=\"heading-anchor\" href=\"#using-your-own-package\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Since we moved the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> directory out of the project, it’s no longer\nworking. We’ll now fix this by installing our new <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code> package.</p>\n<aside class=\"admonition-installing-as-a-user-library admonition\">\n<p class=\"admonition-title\">Installing as a user library</p>\n<p>The following steps install <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code> as a user library. Per-user\ninstalls have a lot of advantages over installing the package system-wide,\nsuch as being usable on systems where you don’t have administrator access\nas well as preventing the package from affecting system services and other\nusers of the machine.</p>\n<p>Note that per-user installations can still affect the behavior of system\ntools that run as that user, so <code class=\"docutils literal notranslate\"><span class=\"pre\">virtualenv</span></code> is a more robust solution\n(see below).</p>\n</aside>\n<ol class=\"arabic\">\n<li><p>To install the package, use pip (you already <a class=\"reference internal\" href=\"#installing-reusable-apps-prerequisites\"><span class=\"std std-ref\">installed it</span></a>, right?):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">pip</span> <span class=\"n\">install</span> <span class=\"o\">--</span><span class=\"n\">user</span> <span class=\"n\">django</span><span class=\"o\">-</span><span class=\"n\">polls</span><span class=\"o\">/</span><span class=\"n\">dist</span><span class=\"o\">/</span><span class=\"n\">django</span><span class=\"o\">-</span><span class=\"n\">polls</span><span class=\"o\">-</span><span class=\"mf\">0.1</span><span class=\"o\">.</span><span class=\"n\">tar</span><span class=\"o\">.</span><span class=\"n\">gz</span>\n</code></pre></div>\n</li>\n<li><p>With luck, your Django project should now work correctly again. Run the\nserver again to confirm this.</p></li>\n<li><p>To uninstall the package, use pip:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">pip</span> <span class=\"n\">uninstall</span> <span class=\"n\">django</span><span class=\"o\">-</span><span class=\"n\">polls</span>\n</code></pre></div>\n</li>\n</ol>\n</section>\n<section id=\"publishing-your-app\">\n<h2>Publishing your app<a class=\"heading-anchor\" href=\"#publishing-your-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now that we’ve packaged and tested <code class=\"docutils literal notranslate\"><span class=\"pre\">django-polls</span></code>, it’s ready to share with\nthe world! If this wasn’t just an example, you could now:</p>\n<ul class=\"simple\">\n<li><p>Email the package to a friend.</p></li>\n<li><p>Upload the package on your website.</p></li>\n<li><p>Post the package on a public repository, such as <a class=\"reference external\" href=\"https://pypi.python.org/pypi\">the Python Package Index\n(PyPI)</a>. <a class=\"reference external\" href=\"https://packaging.python.org\">packaging.python.org</a> has <a class=\"reference external\" href=\"https://packaging.python.org/tutorials/packaging-projects/#uploading-the-distribution-archives\">a good\ntutorial</a>\nfor doing this.</p></li>\n</ul>\n</section>\n<section id=\"installing-python-packages-with-virtualenv\">\n<h2>Installing Python packages with virtualenv<a class=\"heading-anchor\" href=\"#installing-python-packages-with-virtualenv\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Earlier, we installed the polls app as a user library. This has some\ndisadvantages:</p>\n<ul class=\"simple\">\n<li><p>Modifying the user libraries can affect other Python software on your system.</p></li>\n<li><p>You won’t be able to run multiple versions of this package (or others with\nthe same name).</p></li>\n</ul>\n<p>Typically, these situations only arise once you’re maintaining several Django\nprojects. When they do, the best solution is to use <a class=\"reference external\" href=\"https://virtualenv.pypa.io/\">virtualenv</a>. This tool allows you to maintain multiple\nisolated Python environments, each with its own copy of the libraries and\npackage namespace.</p>\n</section>","rootId":"advanced-tutorial-how-to-write-reusable-apps","toc":[{"title":"Reusability matters","anchor":"reusability-matters","children":[]},{"title":"Your project and your reusable app","anchor":"your-project-and-your-reusable-app","children":[]},{"title":"Installing some prerequisites","anchor":"installing-some-prerequisites","children":[]},{"title":"Packaging your app","anchor":"packaging-your-app","children":[]},{"title":"Using your own package","anchor":"using-your-own-package","children":[]},{"title":"Publishing your app","anchor":"publishing-your-app","children":[]},{"title":"Installing Python packages with virtualenv","anchor":"installing-python-packages-with-virtualenv","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/1.11/intro/"}],"prev":{"docname":"intro/tutorial07","title":"Writing your first Django app, part 7","url":"/en/1.11/intro/tutorial07/"},"next":{"docname":"intro/whatsnext","title":"What to read next","url":"/en/1.11/intro/whatsnext/"},"formats":{"html":"/en/1.11/intro/reusable-apps/","markdown":"/en/1.11/intro/reusable-apps.md","json":"/en/1.11/intro/reusable-apps.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/intro/reusable-apps.txt","official":"https://docs.djangoproject.com/en/1.11/intro/reusable-apps/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}