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: median1 = numbers[len(numbers) // 2] median2 = numbers[len(numbers) // 2 - 1] mymedian = (median1 + median2) / 2 else: mymedian = numbers[len(numbers) // 2] return mymedian
Finally, we have to create a file called __init__.py
that imports the two modules.
The “__init__.py” file:
from . import basic from . import stats
The structure of the package is:
.
├── mymath
│ ├── basic.py
│ ├── __init__.py
│ └── stats.py
The package is ready for use. We test it by running one function of each module.
The “test_my_package.py“:
import mymath print("5+6 is equal to:\n") print(mymath.basic.add(5,6)) print("\n\nThe mean of 4,5,6 is equal to:\n") print(mymath.stats.mean([4,5,6]))
If we run the script:
python test_my_package.py
We get:
5+6 is equal to:
11
The mean of 4,5,6 is equal to:
5
Creating pyproject.toml
If we want to build a package, we should work with a pyproject.toml
file. The pyproject.toml
can be:
[build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] name = "first_package" version = "0.0.1" authors = [ { name="Example Author", email="[email protected]" }, ] description = "A small example package" readme = "README.md" requires-python = ">=3.5" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] [project.urls] "Homepage" = "https://github.com/pypa/sampleproject" "Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
Keep in mind that the pyproject.toml
should on the parent directory such as:
Generally, the format should be the following:
packaging_tutorial/ ├── LICENSE ├── pyproject.toml ├── README.md ├── src/ │ └── example_package_YOUR_USERNAME_HERE/ │ ├── __init__.py │ └── example.py └── 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:
The tar.gz
file is a source distribution whereas the .whl
file is a built distribution.
Sources
- [1] Coursera
- [2] Python Packaging