Creating Packages in Python
Learn how to create your own Python package, organize modules and import them into another Python program.
What is a Python Package?
A package is a directory that organizes related Python modules together.
Packages are useful when a project contains many modules and we want to keep related code together.
Why Do We Create Packages?
- Organize large projects.
- Group related modules.
- Reuse code easily.
- Make applications easier to maintain.
- Separate different parts of an application.
Package Creation Diagram
A package can contain multiple Python modules.
Python Project
Main application
Package
student_package
__init__.py
Package initialization file
student.py
Student-related code
marks.py
Marks-related code
main.py
Imports and uses the package modules.
Step 1: Create a Package Folder
First, create a folder for the package.
Example package name:
student_package
student_package/
This folder will contain the related Python modules.
Step 2: Create __init__.py
Inside the package folder, we can create a file named
__init__.py.
student_package/
|
|-- __init__.py
The file can be empty for a basic package.
It can also contain package initialization code when needed.
Step 3: Create a Module
Now create a module called
student.py.
student_package/
|
|-- __init__.py
|
|-- student.py
Add the following code inside
student.py:
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def display(self):
print("Name:", self.name)
print("Course:", self.course)
Step 4: Add Another Module
Let's create another module called
marks.py.
student_package/
|
|-- __init__.py
|
|-- student.py
|
|-- marks.py
Add this code to marks.py:
def calculate_total(marks):
return sum(marks)
def calculate_average(marks):
return sum(marks) / len(marks)
Final Package Structure
myproject/
|
|-- main.py
|
|-- student_package/
|
|-- __init__.py
|
|-- student.py
|
|-- marks.py
Here:
-
myprojectis the project folder. -
student_packageis the package. -
student.pyis one module. -
marks.pyis another module. -
main.pyis the main program.
Importing a Module from a Package
Now we can import the student module
from the package.
In main.py:
from student_package import student
student1 = student.Student(
"Rahul",
"Python"
)
student1.display()
Output:
Name: Rahul
Course: Python
Importing a Class Directly
We can also import the class directly from the module.
from student_package.student import Student
student1 = Student(
"Priya",
"Python Full Stack"
)
student1.display()
Output:
Name: Priya
Course: Python Full Stack
Importing a Function from a Package
We can import functions from a package module.
from student_package.marks import calculate_total
marks = [80, 75, 90, 85]
total = calculate_total(marks)
print("Total:", total)
Output:
Total: 330
Importing Multiple Items
We can import multiple functions from the same module.
from student_package.marks import (
calculate_total,
calculate_average
)
marks = [80, 75, 90, 85]
print("Total:", calculate_total(marks))
print("Average:", calculate_average(marks))
Output:
Total: 330
Average: 82.5
Using __init__.py
The __init__.py file can contain package
initialization code.
For example:
print("student_package loaded")
When the package is imported, this initialization code can run according to how the package is imported.
__init__.py
file. However, __init__.py is still useful
when you want explicit package initialization or when
teaching the traditional package structure.
Package with Multiple Modules
A larger package can contain many related modules.
training/
|
|-- __init__.py
|
|-- students.py
|
|-- courses.py
|
|-- trainers.py
|
|-- fees.py
|
|-- reports.py
Each module has its own responsibility.
This makes the project easier to understand and maintain.
Practical Example
Suppose we create a package called
calculator_package.
Folder structure:
calculator_project/
|
|-- main.py
|
|-- calculator_package/
|
|-- __init__.py
|
|-- addition.py
|
|-- multiplication.py
addition.py
def add(a, b):
return a + b
multiplication.py
def multiply(a, b):
return a * b
main.py
from calculator_package.addition import add
from calculator_package.multiplication import multiply
print(add(10, 20))
print(multiply(5, 4))
Output:
30
20
Module vs Package
| Module | Package |
|---|---|
| Usually one Python file. | Usually a directory that organizes related modules. |
| Contains reusable Python code. | Groups related reusable modules. |
Example:
student.py
|
Example:
student_package/
|
Benefits of Creating Packages
- Better project organization.
- Easy code reuse.
- Related modules stay together.
- Easier maintenance.
- Better separation of responsibilities.
Common Mistakes
- Using the wrong package or module name.
- Writing the wrong import path.
- Creating modules outside the expected package directory.
- Confusing a package name with a module name.
- Running the program from an unexpected project location and getting import errors.
Practice Programs
-
Create a package named
student_package. -
Add
student.pyto the package. -
Add
marks.pyto the package. -
Create a
main.pyfile outside the package. -
Import a class from
student.py. -
Import a function from
marks.py. - Create a second package for calculator operations.
Summary
A Python package is used to organize related modules
into a directory. We can create a package, add an
__init__.py file and create multiple
Python modules inside it. Modules, classes and
functions can then be imported and reused in another
Python program. Packages are especially useful for
keeping large projects organized.