Python Data Persistence – Package

Python Data Persistence – Package

The concept of the package takes Python’s modular approach to next level. A package is a collection of modules. All modules having the functionality of certain common features are stored in one folder. Python’s standard library contains some packages that are collections of modules. For example, the Tkinter package, which is Python’s implementation of the TCL/Tk UI toolkit, consists of several modules such as Tkinter. ttk, Tkinter.messagebox, tkinter.commondialog, and so on. The importlib is another Python package in the standard library that has multiple modules (importlib. ABC, importlib. util, importlib. machinery, and so on.) You can build your own package having multiple but related modules under one folder. Let us see how it can be done.

In addition to Python modules, the folder should have a special file named for it to be recognized as a package by the Python interpreter. This file is also useful to build a packaging list of functions from various modules under the folder.
So, let us build a package named ‘MyPackage’. First of all, create a new folder at some suitable location in a filesystem (for example c:\testpackage) and a subfolder under it. Name the subfolder as ‘MyPackage’. Go on to save the following module scripts in it.

def add ( num1 , num2 ) :
result=num1 + num2
return result
def adda11(*nums) :
tt1 = 0
for num in nums:
return tt1
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
def isprime(num):
for x in range(2,num):
if num%x==0:
return False
return True
if num%2==0:
return True
return False
def isleap(num):
if num%4==0:
return True
return False

In addition to above, create an empty__init__. py in My package folder. The folder structure should appear as shown below (figure 3.17) :

|_ __ _MyPackage

Next, open the command prompt window while in the TestPackage directory and start Python. The child directory in it, MyPackage is recognized as a legitimate Python package because it does, so you can import any module available in it. The usage is shown below:

python >>>


>>> from MyPackage import add functions
>>> add functions.adda11(1,2,3,4)
>>> from MyPackage import functions
>>> functions . is prime (67)
>>> from MyPackage import math functions
>>> math functions.power(10,2)

What is more, it is possible to require a function by name from any module.


>>> from MyPackage.math functions import power,
>>> power ( 5 , 4 )
>>> average ( 20 , 30 )

This is where__init__. py file plays an important role. It can provide package-level access to certain functions chosen from various modules. Let us modify the __init__. py script as shown below:

from. mathfunctions import average
from. addfunctions import adda11
from. myfunctions import isprime, iseven

Now, what does script do? It allows a function to be accessed by name of a package and not by name of a module which originally defined. To verify this behavior, save the following script as ‘’ in the parent folder (‘testpackage’)

import MyPackage
ffcalling isprime function
num=int ( input ( 1 enter a number . . ' ) )
retval=MyPackage . isprime ( num )
if retval==True:
print ( " { } is a prime number " . format ( num ) )
print ( " { } is not a prime number " . format ( num) )
#adda11 function
result=MyPackage.adda11( 10 , 20 )
print ( " Result of addition : " , result )


C:\TestPackage>python enter a number..23 
23 is a prime number 
Result of addition: 30

In the above discussion, we learned how to create a package and use it from inside its parent folder. It is also possible to install the package for system-wide use. However, this book’s scope doesn’t cover its explanation. At this juncture, it is sufficient to state that it can be achieved by using a setup tools module from Python’s standard library. You can also upload your package to the Python Package Index repository ( The procedure is explained in detail on the website itself.
However, it is important to know how to install a new package in your computer’s Python library. The latest versions of Python come with pip utility.