Sometimes you develop a reusable Django app in your project and want to share it across multiple projects—or even publicly via PyPI. Here’s a step-by-step guide on how to convert your Django app into a pip-installable package.
Step 1: Create a Dedicated Django App
- Inside your Django project, create a new app for your reusable functionality:
python manage.py startapp your_app_name
- Do all your coding only inside this app.
- Outside the app, only update
settings.py→INSTALLED_APPSif needed. - Keep the app self-contained so that it can work independently when installed in other projects.
Step 2: Prepare Your App for Packaging
- Once your app is ready, create a new folder outside your Django project with the same name as your app:
mkdir your_app_name
- Copy your Django app into this folder.
- Add the following files to the folder:
-
LICENSE– your license file -
README.md– a brief description of your app -
MANIFEST.in– to include non-Python files -
setup.py– packaging script -
pyproject.toml– build configuration
Example setup.py
from setuptools import setup, find_packages
setup(
name="your_app_name",
version="0.1.0",
packages=find_packages(),
include_package_data=True,
install_requires=[
"Django>=4.2", # specify dependencies
],
license="MIT",
description="A reusable Django app",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/yourusername/your_app_name",
)
Step 3: Build the Package
Run the following command in your app folder:
python -m build
This will create a dist/ folder containing .whl and .tar.gz files.
Step 4: Install Locally
- Go back to your Django project.
- Remove the old app from your project.
- Install the package from the
distfolder:
pip install ../your_app_name/dist/your_app_name-0.1.0-py3-none-any.whl
4.Now your app is installed into your virtual environment and can be used like any other installed package.
Step 5: Publish to PyPI
- Go back to your django app standalone folder,Same folder that you run python -m build.
- Upload to PyPI using Twine:
python -m twine upload dist/*
- It will prompt for your PyPI API token. Generate one from your PyPI account.
- After a successful upload, your app is publicly available! Anyone can install it with:
pip install your_app_name
- Build and test locally before uploading to PyPI.
- After publishing, your app can be reused across projects or shared with the community.
Top comments (0)