How to Develop a Python Package

πŸ“¦ Creating a Python package allows you to share your code with others, organize your projects, and even publish to PyPI for public use.

What is a Python Package? πŸ“¦

A Python package is a collection of Python modules organized in folders with a special __init__.py file. It allows you to:

  • βœ… Organize related code into reusable modules.
  • βœ… Import your code easily into other projects.
  • βœ… Share your functionality with others via PyPI or GitHub.

Think of a package as a folder of tools that you or others can use in multiple projects.

Why Develop a Python Package?

Developing a Python package is useful because it:

  • Promotes code reuse – avoid rewriting the same functions.
  • Simplifies collaboration – others can install and use your package easily.
  • Enables distribution – publish your package to PyPI for global access.
  • Improves organization – structure your project in a professional way.

Developing a Package with PyPI

Here’s a high-level workflow for creating and sharing your Python package.

1. Create a GitHub Repository

Start by creating a GitHub repository to store your package code. This allows version control and collaboration.

  • Give your repository a clear name (usually the same as your package).
  • Add a README.md to describe what your package does.
  • Optionally add a .gitignore to exclude unnecessary files like __pycache__.

2. Folder Structure

Organize your package files so Python can recognize it as a proper package.

A typical structure:

my_package/
β”œβ”€β”€ my_package/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ module1.py
β”‚   └── module2.py
β”œβ”€β”€ tests/
β”‚   └── test_module1.py
β”œβ”€β”€ setup.py
β”œβ”€β”€ pyproject.toml
└── README.md
  • The inner folder my_package/ contains your Python modules.
  • __init__.py tells Python this folder is a package.
  • setup.py or pyproject.toml is needed for packaging and publishing.
Example __init__.py: Calculator Package
# __init__.py
from .module1 import add
Example module1.py: Addition Module
# module1.py
def add(a, b):
    """Return the sum of two numbers."""
    return a + b
Example test_module1.py: Test Addition Module
# test_module1.py
from my_package.module1 import add

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    print("All tests passed!")

if __name__ == "__main__":
    test_add()
Example setup.py: Calculator Package
# setup.py
from setuptools import setup, find_packages

setup(
    name="my_package",
    version="0.1.0",
    packages=find_packages(),
    install_requires=[],
    author="Your Name",
    description="A simple calculator package",
    python_requires=">=3.8",
)
Example pyproject.toml: Calculator Package
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

3. Test & Deploy Package Locally

Before publishing, test your package locally to make sure it works.

  1. Navigate to your root folder.
  2. Install it in editable mode:
pip install -e .
  1. Run your Python code and tests to ensure everything works.
  2. Make changes and re-test until the package runs correctly.

4. Publish Package to PyPI

Once your package is ready, you can share it with the world by publishing to PyPI.

  1. Create an account on PyPI.
  2. Build the distribution:
python setup.py sdist bdist_wheel
  1. Upload to PyPI using twine:
twine upload dist/*
  1. Your package can now be installed via:
pip install my_package