To install the app package requires installation: Setuptools
Installing and uninstalling apps requires installation: PIP
Example polls is an app package. The polls folder contains all the files about the app.
Packaging apps:
-
Firstly, outside of your Django project, polls to create a parent directory. django-polls .
Choose a name for your app
When you choose a name for your package , check the resources in the PyPI to avoid name collisions with existing packages. When creating a package to publish, precede your module name with django- is often useful. This helps other people who are looking for Django apps to differentiate your app specifically for Django.
The applied label (the final part of the point of the applied package) in installed_apps Must be unique in the. Avoid using the same tags as any of the Django contrib package , such as , admin and messages .
-
polls Directory moved to django-polls directory.
-
Create a file containing some content django-polls/ Readme.rst :
Django-polls/readme.rst =====polls=====polls is a simple Django app to conduct Web-based polls. For eachquestion, visitors can choose between a fixed number of answers. Detailed documentation is in the "Docs" directory. Quick start-----------1. Add "Polls" to your Installed_apps setting like this:: Installed_apps = (... ' Polls ',) 2. Include the polls URLconf in your project urls.py like this:: URL (R ' ^polls/', include (' Polls.urls ')), 3. Run ' python manage.py migrate ' to create the polls models.4. Start the development server and visit Http://127.0.0.1:8000/admin/to Create a poll (you ll need the admin app enabled) .5. Visit http://127.0.0.1:8000/polls/to participate in the poll.
Create a django-polls/license file. Choosing License is beyond the scope of this tutorial, but it is worth saying that the publicly released code is useless without license. Django and many Django-compliant apps are released in BSD License; However, you can pick your own License. just know that your license choice will affect who can use your code.
Next we will create a setup.py file that provides detailed information on how to build and install the app. The full explanation of the file is beyond the scope of this tutorial, and the Setuptools documentation is well explained. Create a file django-polls/setup.pywith the following contents:
django-polls/setup.pyImportOsFromSetuptoolsImportSetupWithOpen(Os.Path.Join(Os.Path.DirName(__file__),' Readme.rst '))AsReadme:Readme=Readme.Read()# Allow setup.py to be run from any pathOs.ChDir(Os.Path.Normpath(Os.Path.Join(Os.Path.Abspath(__file__),Os.Pardir)))Setup(Name=' Django-polls ',Version=' 0.1 ',Packages=[' Polls '],Include_package_data=True,License=' BSD License ',# example LicenseDescription=' A simple Django app to conduct web-based polls. ',Long_description=Readme,Url=' http://www.example.com/',Author=' Your Name ',Author_email=' [Email protected] ',classifiers=[' Environment:: WEB Environment ',' Framework:: Django ' "intended audience:: Developers ' ' License:: OSI Approved:: BSD License ' # example License ' Operating System:: OS indep Endent ' ' programming Language:: Python ' # Replace These appropriately if you is stuck on Python 2. ' programming Language:: Python:: 3 ' ' programming Language:: Python: : 3.2 ' ' programming Language:: Python:: 3.3 ' Topic:: Internet:: Www/http ' ' Topic:: Internet:: www/http:: Dynamic Content ' ],)
-
by default only Python modules and packages are included in the package. If you need to include additional files, we need to create a manifest.in file. readme.rst and our license file, create a file django-polls/manifest.in , which reads as follows:
django-polls/manifest.in include Licenseinclude readme.rstrecursive-include polls/static *recursive-include polls/templates *
It is optional to include detailed documentation into your application, but it is recommended that you do so. Create an empty directory Django-polls/docs for storing documents in the future. Add another line to django-polls/manifest.in :
Recursive-include Docs *
Note that docs will not be included in your package unless you add some files underneath it. Many Django applications also provide their online documentation through sites such as readthedocs.org.
Try building your package from the setup.py sdist in Python (run from inside the django-polls ). This creates a dist directory and builds a new package:django-polls-0.1.tar.gz.
For more information on packaging, see the tutorials for packaging and distributing items in Python.
To install and uninstall your own package:
Because we moved the polls directory out of the project directory, it no longer works. We will fix it by installing our new Django-polls package.
A library that is installed as a user
The following steps will install the django-polls into a user's library. User-level installations have many advantages over system-level installations, such as running packages at the normal user level without impacting system services and not affecting other users
Note Depending on the user's installation, System tools that run as the user can still be affected, so virtualenv is a more robust solution (see below).
Install this package and use PIP (you've already installed it, right?). ):
Pip Install--user django-polls/dist/django-polls-0.1.tar.gz
Fortunately, your Django project should now be working again. Please rerun the server to confirm this.
To uninstall this package, use PIP:
Pip Uninstall Django-polls
Packaging and application installation and uninstallation for Django applications