Python - Build Your Own Package

Module

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. [1]

Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

1
2
3
4
import numpy as np
print(np.__name__)

>> numpy

Package

A module can contain multiple objects, such as classes, functions, etc. A package can contain one or more relevant modules. Physically, a package is actually a folder containing one or more module files. [2]

Python package can have sub-packages and modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.

Packages allow hierarchical structure of the module namespace. One example package as below,

image
Here we have four directories, my_model, training, submission and metrics, among which, the last three ones are the subpackage of the first one, my_model. And within all packages and subpackages, the files ending with .py are modules. For each package, we need __inint__.py to indicate the entry of it. [3]



Python - Build Your Own Package
http://arwenzhou.github.io/2022/08/23/Python-Build own package/
Author
Arwen
Posted on
August 23, 2022
Licensed under