How to create a reusable package and distribute it with PIP

Published:

Overview

This article is an overview of the basic process to help to create a general purpose Django app and use it with PIP.

1. Code

Create the app with /original-project$ python manage.py startapp my_new_app in an existing project, then move it to a new directory separated from the project lik /django-new-app.

More at https://docs.djangoproject.com/en/3.2/intro/reusable-apps/

Add tests as a new app in django-new-app.

So we have:

original-project/
django-new-app/
	- my-new-app
	- tests

Have a look at a popular Django app to see how they structure the project, like django-filter: https://github.com/carltongibson/django-filter

2. Package

To use it in original-project, we need to package the application (building a source distribution[^source-dist] for your package) with setup.py: https://setuptools.readthedocs.io/en/latest/userguide/quickstart.html

After you are able to create the package with ``python3.8 setup.py sdistyou will have a compressed file (**Built Distribution**[^built-dist]) indjango-new-app/dist`

https://packaging.python.org/glossary/#term-Built-Distribution To include non module files, create a MANIFEST.in file. 1

Handle dependencies

3. Use it

In our original-project now we can use it with python -m pip install /django-new-app/ or add --user if you are not already using a virtual environment.

4. Publish to pypi.org (Optional)

Guides:

References


  1. https://packaging.python.org/guides/using-manifest-in/ ↩︎

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Steps to create an app in Django, use it with pip, and publishing to pypi

Clutter-free software concepts.
Translations English Espa簽ol

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·