Unit_3_Python_Modules_Packages
Unit_3_Python_Modules_Packages
As our program grows bigger, it may contain many lines of code. Instead of putting everything in
a single file, we can use modules to separate codes in separate files as per their functionality.
Module is a file that contains code to perform a specific task. A module may contain variables,
functions, classes etc.
A module is simply a Python file with a .py extension that can be imported inside another Python
program.
1. Built-in python modules: Python built-in modules are a set of libraries that come pre-
installed with the Python installation. These modules provide a wide range of
functionalities, from file operations and system-related tasks to mathematical
computations and web services. Some examples of Python built-in modules include “os”,
“sys”, “math”, and “datetime”. The Python standard library contains well over 200
modules. We can import a module according to our needs.
a. Python - Math Module: Some of the most popular mathematical functions are
defined in the math module.
• Python from...import statement: We can import specific names from a module without
importing the module as a whole.
# import only pi from math module
from math import pi
print(pi)
• Import all names: In Python, we can import all names(definitions) from a module using
the following construct:
# import all names from the standard module math
from math import *
print("The value of pi is", pi)
b. Python - OS Module: It is possible to automatically perform many operating system
tasks. Python has a built-in os module with methods for interacting with the operating
system, like creating files and directories, management of files and directories, input,
output, environment variables, process management, etc.
import os
os.mkdir("d:\\tempdir")
c. Python - Random Module: Python has a built-in module that you can use to make
random numbers.
import random
print(random.random())
2. User-defined python modules: The user-defined module is written by the user at the
time of program writing.
• Creation a User-defined Module: To create a module just write a Python code in a file
with file extension as .py
Example:
# Python Module addition- example.py
def add(a, b):
result = a + b
return result
• Accessing a User-defined Module: Now we will access the module that we created
earlier, by using the import statement.
#accessing module- module_1.py
import example
print(example.add(4,5))
Python Packages
While working on big projects, we have to deal with a large amount of code, and writing
everything together in the same file will make our code look messy. We can separate our code
into multiple files by keeping the related code together in packages.
Python packages are a way to organize and structure code by grouping related modules into
directories. A package is essentially a folder that contains an __init__.py file and one or more
Python files (modules). This organization helps manage and reuse code effectively, especially in
larger projects.
Example: In this example, we are creating a Math Operation Package to organize Python code
into a structured package with two sub-packages: basic (for addition and subtraction) and
advanced (for multiplication and division).
• math_operations/__init__.py
• math_operations/basic/__init__.py:
from .add import addition
from .sub import subtract
• math_operations/basic/add.py:
• math_operations/basic/sub.py:
• math_operations/advanced/__init__.py:
from .mul import multiply
from .div import divide
• math_operations/advanced/mul.py:
• package_example.py
from math_operations import addition, subtract, multiply, divide
# Perform basic operations
print("Addition:", addition(5, 3))
print("Subtraction:", subtract(10, 4))
print("Multiplication:", multiply(4, 2))
print("Division", divide(20, 4))
Python Packages Examples