Skip to content

Creating Python Packages: Detailed Overview and Examples

Creating Python packages is an essential skill for organizing and distributing Python code. A package is a directory containing modules and a special file named __init__.py. This report provides a detailed guide on creating and managing Python packages, including the necessary directory structure, packaging, and distribution.

1. Understanding Python Packages

A Python package is a directory containing Python modules and a special __init__.py file, which makes the directory recognizable as a package. Packages help in organizing code into modules and sub-packages, making it easier to manage and reuse.

Basic Package Structure

my_package/
├── my_module.py
├── __init__.py
├── sub_package/
│   ├── __init__.py
│   └── sub_module.py
└── setup.py

2. Creating a Basic Package

Example: Simple Package Structure

  1. Create the Package Directory

Create a directory for your package:

mkdir my_package
cd my_package
  1. Add Modules and Sub-packages

Create Python files inside the package directory:

# my_package/my_module.py
def hello():
    return "Hello from my_module!"

# my_package/sub_package/sub_module.py
def greet(name):
    return f"Hello, {name}!"
  1. Create __init__.py

Create an __init__.py file to mark the directory as a package:

# my_package/__init__.py
from .my_module import hello
from .sub_package.sub_module import greet

3. Adding Metadata with setup.py

The setup.py file is used to define the package metadata and configuration needed for distribution.

Example: setup.py File

from setuptools import setup, find_packages

setup(
    name='my_package',
    version='0.1',
    description='A sample Python package',
    author='Your Name',
    author_email='yourname@example.com',
    url='https://github.com/yourusername/my_package',
    packages=find_packages(),
    install_requires=[],
    classifiers=[
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: MIT License',
    ],
)

Key Parameters

  • name: The name of the package.
  • version: The version number of the package.
  • description: A short description of the package.
  • author and author_email: Contact information for the package author.
  • url: The URL for the project’s homepage or repository.
  • packages: Specifies which packages to include.
  • install_requires: Lists dependencies required by the package.
  • classifiers: Provides metadata about the package’s compatibility and licensing.

4. Building and Distributing Your Package

Building the Package

To build the package distribution archives, use the following commands:

python setup.py sdist bdist_wheel
  • sdist generates a source distribution.
  • bdist_wheel generates a wheel distribution.

Installing the Package Locally

To install the package locally for testing:

pip install .

This command installs the package from the current directory.

Uploading to PyPI

To distribute your package publicly, upload it to PyPI (Python Package Index):

  1. Install twine
pip install twine
  1. Upload the Package
twine upload dist/*

5. Package Example with Extra Features

Adding Data Files

If your package includes non-Python files, specify them using package_data or data_files:

setup(
    ...
    package_data={
        'my_package': ['data/*.dat'],
    },
)

Including Scripts

If your package includes executable scripts, define them using the entry_points argument:

setup(
    ...
    entry_points={
        'console_scripts': [
            'my_command=my_package.cli:main',
        ],
    },
)

Defining Dependencies

List external dependencies in the install_requires argument:

setup(
    ...
    install_requires=[
        'numpy>=1.18.0',
        'requests>=2.24.0',
    ],
)

6. Creating a Package with pyproject.toml

As an alternative to setup.py, you can use pyproject.toml for packaging configuration:

Example: pyproject.toml

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "my_package"
version = "0.1"
description = "A sample Python package"
authors = [{name = "Your Name", email = "yourname@example.com"}]
dependencies = []

Building with pyproject.toml

To build the package with pyproject.toml:

python -m build

Summary

Creating a Python package involves organizing your code into a structured directory, defining metadata and configuration in setup.py or pyproject.toml, and building and distributing the package. By following these steps, you can create well-organized, reusable Python packages and share them with the Python community or use them in your projects.