How to publish a Django package to PyPi?
pip
I am sure that you’ve installed packages using pip while developing a project using Django. So the question here is how do you make one of your functions or a even a project reusable just like other pip packages you have used?
pip install django-package-by-me
How do you do this?
PyPi
PyPi or also named as The Python Package Index. This is where all of our pip packages reside. The pip packages we use inside our Django project or rather any python project is published in PyPi. So PyPi is the where you should upload our package. django app directories
We are covering the below questions.
- How can I make a django package
- How to upload it to PyPi
Making a django package
Your django project should be ready for packaging (up and running).
Let’s consider that the name of the package you’re going to deploy as django-polls
. And your django project is polls
- Create a parent directory named
django-polls
and create another directory namedpolls
- Copy the django app directories you want to package into the
polls
directory. - Create a file
django-polls/README.rst
and add following contentdjango-polls/README.rst
with the following contents (This is for documentation purpose):
=====
Heading
=====Description hereDetailed 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).https://www.youtube.com/watch?v=YIdXBq2_RN85. Visit http://127.0.0.1:8000/polls/ to participate in the poll.
4 . Also create a LICENSE
file in the same directory
5. Create a setup.cfg
file
[metadata]
name = django-polls
version = 0.1
description = A Django app to conduct Web-based polls.
long_description = file: README.rst
url = https://www.example.com/
author = Your Name
author_email = yourname@example.com
license = BSD-3-Clause # Example license
classifiers =
Environment :: Web Environment
Framework :: Django
Framework :: Django :: X.Y # Replace "X.Y" as appropriate
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Internet :: WWW/HTTP
Topic :: Internet :: WWW/HTTP :: Dynamic Content
[options]
include_package_data = true
packages = find:
6. Create a setup.py
file
from setuptools import setup
setup()
7. Create MANIFEST.in
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
The directory structure will be like
django-polls
— README.rst [file]
— LICENSE [file]
— setup.cfg [file]
— setup.py [file]
— MANIFEST.in [file]
— polls [dir]
— — [app directories]
— — ..
— — ..
8. python setup.py sdist
to build the package
Now you may see some changes in the directory
9. Install twine pip install twine
10. Create an account on PyPi
11. twine upload dist/*
Enter PyPi username and password
Now you’re done with uploading the package. You can now install the package using pip install