Building Own Python Module - Python Programming by Example (2015)

Python Programming by Example (2015)

10. Building Own Python Module

This chapter explains how to build own Python module.

10.1 Creating Simple Module

In this section, we create a simple module. We will call functions from external file (*.py) in the same package, main package.

Firstly, create a file,, and write these scripts.

def perform(a, b):

return a * 2.5 + b

def calculate(a,b):

return a + b * 5

You also can create a class on Python file, for instance and write these scripts.

from math import *

class City:

# class data

city_count = 0

city_id = 0

# constructor

def __init__(self, name='', x=0, y=0): = name

self.x = x

self.y = y

City.city_count += 1 # access all City classes

self.city_id = City.city_count

def __str__(self):

return 'City: ' + + ',id=' + str(self.city_id) + ',x=' + str(self.x) + ',y=' + str(self.y)

# class attributes

def move_to(self, x=0, y=0):

self.x += x

self.y += y

def distance(self, other_city):

xi = pow(other_city.x - self.x, 2)

yi = pow(other_city.y - self.y, 2)

return sqrt(xi + yi)

def __del__(self):

# get class name

class_name = self.__class__.__name__

print('class ', class_name, ' destroyed')

Now you access functions from and files using import statement.

Write these scripts.

# access our modules

import simplemodule

import simpleadvmodule

# use simplemodule

num1 = simplemodule.perform(10, 5)


num1 = simplemodule.calculate(4, 3)


# use simpleadvmodule

city_a = simpleadvmodule.City('Hamburg', 8, 12)

city_b = simpleadvmodule.City('Berlin', 5, 7)



city_a.move_to(4, 3)

city_b.move_to(7, 12)



Save into a file, called Run the program.

$ python3

Program output:


10.2 Building Own Python Package

In previous program, we create a module in Python package. We create a package and then use it in our program.

You can create a package, called Learning, by creating folder. Inside Learning folder, you create folders: Algebra and Arithmetic.

The following is our package structure.


Now we add several files in each folder

· Learning folder:,, and

· Learning/Algebra folder: and

· Learning/Arithmetic folder: and

The following is script implementation for each file.

def do_something():

print('call do_something()')

class Computer:

# constructor

def __init__(self, name=''): = name

def __str__(self):

return 'Computer: ' +

def say_hello(self):

print("I'm computer, called",

def foo():

print('call foo()') from Learning folder.

from common import do_something

from Computer import Computer

from foo import foo

def add(a, b):

return a + b from Learning/Algebra folder.

from Learning.Algebra.add import add

def calculate(a, b):

return a + b * 2.8 from Learning/Arithmetic folder.

from Learning.Arithmetic.calculate import calculate

Now we can access our package. Create a file, called, and write these scripts.

import sys


import Learning


a = Learning.Computer('myPC')


import Learning.Algebra as algebra

b = algebra.add(10, 5)


import Learning.Arithmetic as arith

c = arith.calculate(5, 8)


Save and run the program.

$ python3

Program output: