Module 04
Modules, packages and file handling
Modules
Python Modules are files that contain Python code, like functions, variables, or
classes, which help us organize programs into smaller, reusable pieces. They
make our code cleaner and easier to manage. Modules in Python are useful
when we want to break large programs into logical parts or reuse code across
different files.
Features of Python Modules
● Help break code into reusable, organized parts.
● Can store functions, variables, and classes.
● Make large programs easier to manage.
● Support importing specific or all components.
● Allow code sharing across multiple Python files.
Create a Python Module
Creating a Python module is as simple as saving a Python file with a .py
extension.
For example, create a file called [Link]:
# [Link]
def add(a, b):
return a + b
def subtract(a, b):
return a - b
In this example, we create a Python module named [Link] that has two
functions : add and subtract.
Types of Modules in Python
There are two main types of modules we can use in our programs:
1. Built-in Modules in Python
Python built-in modules are ready-to-use libraries that come with Python. They
help us perform common tasks like math, random number generation, and
working with dates without extra installation.
Example:
import math
print([Link](25))
In this example, we use the math module to find the square root of 25.
2. User-defined Modules in Python
User-defined Python modules that we create ourselves. We write a .py file
containing functions, variables, or classes, and then import that file into other
Python programs to reuse the code.
Example:
Step 1: Create a module file ([Link])
# [Link]
def say_hello(name):
return f"Hello, {name}!"
Step 2: Use the module in another file ([Link])
# [Link]
import greetings
print(greetings.say_hello("Riya"))
Output:
Hello, Riya!
In this example, we define a simple function in a user-defined module and
import it into another file to reuse the greeting logic.
Variables in Python Modules
Apart from functions and classes, Python modules contain variables, such as
dictionaries, tuples, objects, lists, etc.
Example:
# [Link]
app_name = "Task Manager"
version = 1.0
features = ["Add Task", "Edit Task", "Delete Task"]
In this example, we define three variables in a module file: a string (app_name),
a number (version), and a list (features). These variables can be accessed from
another file when we use the module.
Import Modules in Python
We use the import statement to bring code from one file into another. It helps
reuse functions or variables easily. This is how we import modules in Python.
Syntax:
import module_name
This syntax is used only to import the entire module, not individual functions or
classes. To access anything inside the module, we use the dot (.) operator like
module_name.function_name.
Example:
We have a module file named [Link] that contains a simple function.
# [Link]
def square(num):
return num * num
Now, we use this module in another file called [Link].
# [Link]
import mathutils
result = [Link](6)
print("Square:", result)
Output:
Square: 36
In this example, we import the entire mathutils module. Then we access the
square() function using the dot . operator: [Link](6). This shows how
to reuse code from another Python file using the import statement.
Python Import From Module
Here we explore how to selectively or fully access content from Python
Modules using different forms of the import statement.
1. Import Specific Attributes from a Python module
We use the from statement in Python to import specific attributes from a
module, such as functions, variables, or classes, without importing the entire
module.
To import a specific function/variable/class, we use from <module_name>
import <class/function/variable_name> statement. It will allow us to import a
specific resource from that module.
Syntax:
from <module_name> import <function_name>, <class_name>,
<variable_name>
Example:
Let’s create a module named [Link]:
# [Link]
def square(x):
return x * x
def cube(x):
return x * x * x
pi = 3.14159
Now, let’s create another file, [Link], where we import specific items in the
code:
# [Link]
from mathutils import square, pi
print("Square of 5:", square(5))
print("Value of pi:", pi)
Output:
Square of 5: 25
Value of pi: 3.14159
In this example, we import only the square function and the pi variable from the
mathutils module. This allows us to use them directly, without writing
mathutils. before them.
2. Import All Attributes from a Python Module (import *)
In Python, we can use the from module_name import * statement to import all
public attributes, like functions, variables, and classes, from a module at once.
This allows us to use them directly without the module name.
Syntax:
from module_name import *
Example:
Let’s say we have a module named [Link]:
# [Link]
def square(x):
return x * x
def cube(x):
return x * x * x
pi = 3.14159
Now, in [Link], we can import all the attributes:
# [Link]
from mathutils import *
print("Square of 4:", square(4))
print("Cube of 2:", cube(2))
print("Value of pi:", pi)
Output:
Square of 4: 16
Cube of 2: 8
Value of pi: 3.14159
In this example, we use import * to get all functions and variables from the
module.
Import Python Standard Library Modules
The standard library in Python comprises over 200 modules that we can import
based on our requirements.
Example: Using the math and datetime Modules
import math
import datetime
# Using math module
print("Square root of 49 is:", [Link](49))
# Using datetime module
current_time = [Link]()
print("Current date and time is:", current_time)
In this example, we import two standard library modules: math and datetime.
The math module provides mathematical operations, while the datetime module
handles date and time functions.
Locating Python Modules
When we use the import statement, Python looks for the module in a specific
order using the [Link] list.
Python searches in this order:
1. It checks the current working directory first.
2. If not found, it searches directories listed in the PYTHONPATH
environment variable.
3. Finally, it checks the default system directories set during Python
installation.
Example:
import sys
# Print all paths Python searches for modules
for path in [Link]:
print(path)
Output:
/home/user/my_project
/usr/lib/python3.10
/usr/lib/python3.10/lib-dynload
/usr/local/lib/python3.10/dist-packages
/usr/lib/python3/dist-packages
Note: The output will vary depending on our system and Python version.
Directories List for Python Modules
[Link] is a built-in variable in the sys module and contains a list of directories
that the Python interpreter searches for the required module.
Example:
import sys
for path in [Link]:
print(path)
Output:
/your/script/location
/usr/lib/python3.x
/usr/lib/python3.x/lib-dynload
/usr/local/lib/python3.x/site-packages
…
These are the directories Python checks to find and load your modules. You can
also add custom paths to [Link] if needed.
Renaming the Python Module
Python allows us to change the names of Python modules when importing them.
Renaming a Python module is an easy and flexible method. We can rename the
module with a specific name and use it to call the module resources. We use the
‘as’ keyword for renaming.
Syntax:
Import Module_name as Alias_name
Example:
import pandas as pd
df = [Link](
{
"Name" : ["A", "B", "C"],
"Age" : [10, 20, 30]
}
)
print(df)
Run Code
Output:
Name Age
0 A 10
1 B 20
2 C 30
Reloading a Python Module
Sometimes while testing or developing, we make changes to a module after it’s
already been imported. To apply those changes without restarting the interpreter,
we can reload the module.
We use the importlib module’s reload() function:
import importlib
import module_name
[Link](module_name)
Example:
Let’s say we made changes to a module named [Link].
import importlib
import utils
# reload the updated module
[Link](utils)
By reloading, we make sure the latest changes in [Link] are active without
restarting Python.
Packages
Python packages are a way to organize related modules (single .py files) into a
hierarchical directory structure, much like folders and subfolders in a file
system. They help manage complex projects, prevent naming conflicts, and
allow for code reusability and distribution.
How to Create a Package in Python?
Creating a Python package is a simple process. It allows us to organize code
into manageable and reusable modules. Here are the steps to create packages in
Python:
Step 1: Create the Package Directory
The first step is to create a directory for the package with the desired name. It
will serve as a foundation for the structure of the package.
Step 2: Add Python Modules
In the directory, add Python files or modules containing code. Every module
represents a distinct functionality of the package.
Step 3: Create __init__.py File
Create an empty __init__.py file within the package directory. The file can also
contain the initialization code for the package and signifies its status as a
package.
Step 4: Create Sub-Packages (Optional)
Create sub-packages within the package by adding more directories that contain
modules with __init__.py files.
Step 5: Add Relevant Module Files
Introduce module files to the directory, and modules will store code relevant to
the functionality of the package.
Step 6: Import Using Dot Notation
Import modules into Python scripts using dot notation.
from mypackage import mymodule
Step 7: Create [Link] for Distribution
To distribute the package to others, create a [Link] file using the setuptools
library in Python. The [Link] file defines metadata about the package and
mentions how to install it.
Example:
Here’s a complete example of how to create a Python package step-by-step:
1. Create the Directory Structure
Let’s say we want to create a package called mypackage. The structure would
look like this:
mypackage/
│
├── __init__.py
├── [Link]
├── [Link]
│
└── subpackage/
├── __init__.py
└── [Link]
2. Add Code to [Link] and [Link]
We now add some simple functions to our modules:
mypackage/[Link]
def greet():
return "Hello from module 1!"
mypackage/[Link]
def farewell():
return "Goodbye from module 2!"
3. Create the __init__.py File
We use the __init__.py file to make mypackage a proper Python package. We
can also define imports here to simplify access.
mypackage/__init__.py
from .module1 import greet
from .module2 import farewell
This allows us to call greet() and farewell() directly from mypackage.
4. Create a Subpackage
We can also create subpackages by adding more directories that contain
__init__.py files.
mypackage/subpackage/__init__.py
# This can be empty or include submodule imports
mypackage/subpackage/[Link]
def submodule_greet():
return "Hello from submodule 1!"
5. Use the Package in a Script
Now, we can use the package in our Python script.
import mypackage
print([Link]()) # Output: Hello from module 1!
print([Link]()) # Output: Goodbye from module 2!
from [Link] import submodule1
print(submodule1.submodule_greet()) # Output: Hello from submodule 1!
6. Distribute the Package
To distribute the package, create a [Link] file. This file will contain metadata
and instructions for installing the package.
from setuptools import setup, find_packages
setup(
name="mypackage",
version="0.1",
packages=find_packages(),
author="Your Name",
description="A simple example package",
license="MIT",
keywords="example package",
)
To install the package locally, we run this command in the terminal (inside the
folder where [Link] is located):
pip install .
Importing Module from a Python Package
Python packages enhance code reusability. To access a module from a package,
we use the import statement in the Python source file where we want to access
it.
Using the dot(.) operator, we can import modules from packages in Python.
When we import modules and packages, we can use existing functions that can
speed up our work.
Syntax:
import module1[, module2,... moduleN]
In the syntax,
Here, import is the keyword to import the module.
module1 is the module we want to import.
Modules enclosed in brackets are optional; we mention them when we want to
import more than 1 module.
Example:
import mypackage.module1
import mypackage.module2
print([Link]()) # Output: Hello from module 1!
print([Link]()) # Output: Goodbye from module 2!
Installing a Python Package Globally
Once we create a package, we can make it available system-wide by running the
setup script using the setup() function from Python’s setuptools module.
Step 1: Upgrade pip and setuptools
Before packaging, make sure we’re using the latest versions of pip and
setuptools. Run the following command:
python -m pip install --upgrade pip setuptools
Step 2: Create the [Link] File
Inside the main package folder, create a file named [Link]. Use the setup()
function, where we define:
name
version
license
description
author
zip_safe (to specify if the package can be safely compressed)
Using the Installed Python Package
Once the package is installed globally, we can import and use its modules or
functions directly in our Python code.
Import Without Package Prefix
We can import specific modules or functions directly without using the package
prefix.
Example:
from mypackage.module1 import greet
from mypackage.module2 import farewell
print(greet()) # Output: Hello from module 1!
print(farewell()) # Output: Goodbye from module 2!
Import Only Required Functions
Instead of importing the entire module, we can import just the required
functions for cleaner and more efficient code.
Example:
# Import only the required functions from the modules
from mypackage.module1 import greet
from mypackage.module2 import farewell
# Use the functions without referring to the module name
print(greet()) # Output: Hello from module 1!
print(farewell()) # Output: Goodbye from module 2!
Show the steps to create a package (Engg), sub-package( years), module(sem)
and create staff and student functions to the module.
To create a nested package structure in Python (Engg -> Years -> Sem) and
define specific functions, you need to create a hierarchy of directories, each
containing an __init__.py
Step 1: Create the Directory Structure
Create the required folders and add a (potentially empty) __init__.py file in each
directory to mark them as Python packages
# Navigate to your project's root directory in the terminal
# Create the main 'Engg' package
mkdir Engg
cd Engg
touch __init__.py
# Create the 'Years' sub-package (e.g., 'FirstYear')
mkdir FirstYear
cd FirstYear
touch __init__.py
# Create the 'Sem' sub-package (e.g., 'Sem1')
mkdir Sem1
cd Sem1
touch __init__.py
# Return to the root to continue
cd ../../
The final structure will look like this:
Engg/
│
├── __init__.py
│
└── FirstYear/
│
├── __init__.py
│
└── Sem1/
│
├── __init__.py
└── academic_functions.py
Step 2: Define Functions in a Module
Inside the Engg/FirstYear/Sem1/ directory, create a Python module (e.g.,
academic_functions.py) and define the staff and student functions within it
using the def keyword.
File: Engg/FirstYear/Sem1/academic_functions.py
def staff_function():
"""Prints a message for staff members."""
print("Welcome, Staff member! This is the First Year, First Semester
module.")
def student_function():
"""Prints a message for students."""
print("Hello, Student! This is the First Year, First Semester module.")
Step 3: Use the Package and Functions
You can now import and use these functions in another Python script (e.g.,
[Link]) by using dot notation to navigate the package hierarchy.
File: [Link]
# Import specific functions from the nested module
from [Link].Sem1.academic_functions import staff_function,
student_function
# Call the functions
staff_function()
student_function()
When you run [Link]
Output:
Welcome, Staff member! This is the First Year, First Semester module.
Hello, Student! This is the First Year, First Semester module.
Files
To store data in a computer, we need files. In computers’ view, a file is nothing
but collection of data that is available to a program. Once we store data in a
computer file, we can retrieve it and use it depending on our requirements.
There are four important advantages of storing data in a file:
1. When the data is stored in a file, it is stored permanently. This means that
even though the computer is switched off, the data is not removed from
the memory since the file is stored on a hard disk or CD. This file data
can be utilized later, whenever required.
2. It is possible to update the file data. For example, we can add new data to
the existing file, delete unnecessary data from the file and modify the
available data of the file. This makes the file more useful.
3. Once the data is stored in a file, the same data can be shared by various
programs. For example, once employee data is stored in a file, it can be
used in a program to calculate employees’ net salaries or in another
program to calculate income tax payable by the employees.
4. Files are highly useful to store huge amounts of data. For example,
voters’ lists or census data.
Types of Files in Python
In Python, there are two types of files. They are:
Text files
Binary files
Text files
Text files store the data in the form of characters. For example, if we store the
employee name “Ganesh”, it will be stored as 6 characters and the employee
salary 8900.75 is stored as 7 characters. Normally, text files are used to store
characters or strings.
Binary files
Binary files store entire data in the form of bytes, i.e. a group of 8 bits each. For
example, a character is stored as a byte and an integer is stored in the form of 8
bytes. When the data is retrieved from the binary file, the programmer can
retrieve the data as bytes. Binary files can be used to store text, images, audio
and video.
It is very important to know how to create files, store data in the files and
retrieve the data from the files in Python. To do any operation on files, first of
all we should open the files.
Reference
Modules
[Link]
Packages:
[Link]