Predictive Hacks

How to Build Python Packages

python package

In this tutorial, we will show you how to build Python Packages. Our goal is to build a Python package called mymath that consists of two modules, such as the basic module that contains three functions and the stats module that contains two functions too.

We have to create a new folder with the name of our package, in this case, “mymath”.

mkdir mymath

Now, within the “mymath” folder, let’s create the “basic” and the “stats” module with file names, and respectively.

The “” file:

def square(number):
    This function returns the square of a given number
    return number ** 2

def double(number):
    This function returns twice the value of a given number
    return number * 2

def add(a, b):
    This function returns the sum of given numbers
    return a + b

The file:

def mean(numbers):
    This function returns the mean of the given list of numbers
    return sum(numbers)/len(numbers)

def median(numbers):
    This function returns median of the given list of numbers
    if len(numbers) % 2 == 0:
       median1 = numbers[len(numbers) // 2]
       median2 = numbers[len(numbers) // 2 - 1]
       mymedian = (median1 + median2) / 2
       mymedian = numbers[len(numbers) // 2]
    return mymedian

Finally, we have to create a file called that imports the two modules.

The “” file:

from . import basic
from . import stats

The structure of the package is:

├── mymath
│   ├──
│   ├──
│   └──

The package is ready for use. We test it by running one function of each module.

The ““:

import mymath

print("5+6 is equal to:\n")

print("\n\nThe mean of 4,5,6 is equal to:\n")

If we run the script:


We get:

5+6 is equal to:


The mean of 4,5,6 is equal to:


Creating pyproject.toml

If we want to build a package, we should work with a pyproject.toml file. The pyproject.toml can be:

requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

name = "first_package"
version = "0.0.1"
authors = [
  { name="Example Author", email="[email protected]" },
description = "A small example package"
readme = ""
requires-python = ">=3.5"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",

"Homepage" = ""
"Bug Tracker" = ""

Keep in mind that the pyproject.toml should on the parent directory such as:

How to Build Python Packages 1

Generally, the format should be the following:

├── pyproject.toml
├── src/
│   └── example_package_YOUR_USERNAME_HERE/
│       ├──
│       └──
└── tests/

Then, we generate distribution packages for the package by running:

python -m pip install --upgrade build
python3 -m build

This command should output a lot of text and once completed should generate two files in the dist directory:

How to Build Python Packages 2

The tar.gz file is a source distribution whereas the .whl file is a built distribution


Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore