DEV Community

Sreethul
Sreethul

Posted on

How to Convert a Django App into a Pip-Installable Package

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

  1. Inside your Django project, create a new app for your reusable functionality:
python manage.py startapp your_app_name
Enter fullscreen mode Exit fullscreen mode
  1. Do all your coding only inside this app.
  2. Outside the app, only update settings.pyINSTALLED_APPS if needed.
  3. Keep the app self-contained so that it can work independently when installed in other projects.

Step 2: Prepare Your App for Packaging

  1. Once your app is ready, create a new folder outside your Django project with the same name as your app:
mkdir your_app_name
Enter fullscreen mode Exit fullscreen mode
  1. Copy your Django app into this folder.
  2. 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",
)
Enter fullscreen mode Exit fullscreen mode

Step 3: Build the Package

Run the following command in your app folder:

python -m build
Enter fullscreen mode Exit fullscreen mode

This will create a dist/ folder containing .whl and .tar.gz files.


Step 4: Install Locally

  1. Go back to your Django project.
  2. Remove the old app from your project.
  3. Install the package from the dist folder:
pip install ../your_app_name/dist/your_app_name-0.1.0-py3-none-any.whl
Enter fullscreen mode Exit fullscreen mode

4.Now your app is installed into your virtual environment and can be used like any other installed package.


Step 5: Publish to PyPI

  1. Go back to your django app standalone folder,Same folder that you run python -m build.
  2. Upload to PyPI using Twine:
python -m twine upload dist/*
Enter fullscreen mode Exit fullscreen mode
  1. It will prompt for your PyPI API token. Generate one from your PyPI account.
  2. After a successful upload, your app is publicly available! Anyone can install it with:
pip install your_app_name
Enter fullscreen mode Exit fullscreen mode

  • 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)