How to Build Python Packages

A tutorial on how to build Python packages

George Pipis
3 min readMar 8, 2023

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, basic.py and stats.py respectively.

The “basic.py” 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 stats.py 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
"""
numbers.sort()

if len(numbers) % 2 == 0…

--

--

George Pipis

Sr. Director, Data Scientist @ Persado | Co-founder of the Data Science blog: https://predictivehacks.com/