Nearby lessons
77 of 159Python - Packages
- Understand what a package is and how a folder becomes a package
- Explain the role of the __init__.py file
- Import an entire module from a package with import package.module
- Import only the required function with from package.module import function
- Work with nested packages and sub-packages
- Describe the hierarchy of library, package, module, function, class, and variable
Packages
A Package is an encapsulation mechanism used to group related modules into a single unit.
A package is nothing but a folder (directory) that represents a collection of Python modules.
What is a Package?
A package is:
- A collection of related Python modules.
- Represented by a folder or directory.
- Used to organize modules in a structured manner.
__init__.py File
Any folder or directory that contains the __init__.py file is considered a Python package.
The __init__.py file can be empty.
Sub-Packages
A package can also contain sub-packages.
The following package structure represents a package with two sub-packages.
Package Structure
Understanding the Package Structure
The above package structure shows the following:
- Loan is the main package.
- Home Loan and Vehicle Loan are sub-packages.
- Each package contains its own
__init__.pyfile. - Each sub-package contains Python modules.
Advantages of Packages
According to the tutorial, the main advantages of packages are:
- We can resolve naming conflicts.
- We can identify our components uniquely.
- It improves the modularity of the application.
Package Hierarchy
Role of __init__.py
According to the tutorial:
- It identifies the directory as a Python package.
- The file may be empty.
- Every package and sub-package contains its own
__init__.pyfile.
Important Interview Questions
- What is a Python package?
- How is a package different from a module?
- Why do we use packages in Python?
- What is the purpose of the
__init__.pyfile? - Can the
__init__.pyfile be empty? - What is a sub-package?
- What are the advantages of using packages?
- How does a package help resolve naming conflicts?
Example 1: Creating and Using a Package
In this example, we will learn how to create a Python package and use the modules stored inside it.
The example demonstrates:
- Creating a package.
- Creating the
__init__.pyfile. - Creating a Python module.
- Importing the entire module.
- Calling a function from the imported module.
Directory Structure
The following directory structure is used in this example.
Understanding the Directory Structure
- test.py is the main Python program.
- pack1 is the Python package.
- __init__.py identifies
pack1as a Python package. - module1.py is a module stored inside the package.
Step 1: Create __init__.py
Create the __init__.py file inside the pack1 directory.
The __init__.py file is an empty file.
__init__.py
Explanation
The __init__.py file makes the pack1 folder a Python package.
The file does not need to contain any code.
Step 2: Create module1.py
Create a module named module1.py inside the pack1 package.
The module contains a function named f1().
module1.py
Explanation
The function f1() simply prints a message.
This function will later be accessed from another Python program.
Version 1: Import the Entire Module
In the first approach, the complete module is imported.
After importing the module, we access its function by using the complete package and module name.
Create test.py
Requires the package structure to exist on disk (not available in the in-browser editor).
import pack1.module1
pack1.module1.f1()Program Explanation
The statement:
import pack1.module1imports the complete module1.py module from the pack1 package.
Since the whole module is imported, the function is accessed by using:
pack1.module1.f1()Output
Output Explanation
Python imports the module1 module from the pack1 package.
The f1() function is called using the complete package path.
Finally, the message stored inside the function is displayed on the screen.
Important Points
- The complete module is imported.
- The package name must be specified while calling the function.
- The module name must also be specified.
- This approach is useful when multiple functions or variables from the module are required.
Flow of Execution
Important Interview Questions
- How do you create a Python package?
- Why is the
__init__.pyfile required? - How do you import an entire module from a package?
- How do you call a function after importing the complete module?
- What is the purpose of the statement
import pack1.module1? - Why do we write
pack1.module1.f1()instead of simplyf1()?
Version 2: Import Only the Required Function
Instead of importing the complete module, Python allows us to import only the required function.
This approach reduces the need to repeatedly write the package and module names while calling the function.
Create test.py
Requires the package structure to exist on disk (not available in the in-browser editor).
from pack1.module1 import f1
f1()Understanding the Import Statement
The statement:
from pack1.module1 import f1imports only the f1() function from the module1.py module.
Since only the required function is imported, there is no need to use the package name or module name while calling it.
Output
Explanation
In this version:
- Only the function
f1()is imported. - Since the function is imported directly, there is no need to use the package or module name.
Instead of writing:
pack1.module1.f1()we can directly write:
f1()Why Direct Function Import?
Directly importing a function makes the program simpler and easier to read.
This approach is useful when only a few functions from a module are required.
It also reduces the amount of code needed to access those functions.
Comparison of Both Versions
| Version | Import Statement | Function Call |
|---|---|---|
| Version 1 | import pack1.module1 |
pack1.module1.f1() |
| Version 2 | from pack1.module1 import f1 |
f1() |
Difference Between Version 1 and Version 2
| Version 1 | Version 2 |
|---|---|
| Imports the complete module. | Imports only the required function. |
| Uses the complete package and module path. | Calls the function directly. |
| Useful when many members are required. | Useful when only one or a few members are required. |
| Function call is longer. | Function call is shorter and easier to read. |
Execution Flow (Version 2)
Best Practice
Choose the import style based on your requirements:
- Use
import package.modulewhen many functions or variables from the module are needed. - Use
from package.module import functionwhen only a few members are required.
Important Interview Questions
- What is the difference between
import package.moduleandfrom package.module import function? - When should we import the complete module?
- When should we import only the required function?
- Why can
f1()be called directly in Version 2? - Which import statement produces shorter code?
- Which version provides direct access to imported functions?
- Can both import styles produce the same output?
- Which approach is easier to read when only one function is needed?
Example 2: Working with Nested Packages
In this example, a package contains another package, which is known as a sub-package or nested package.
This example demonstrates how to:
- Create a main package.
- Create a sub-package inside the main package.
- Create modules inside both packages.
- Import functions from the main package and the sub-package.
- Execute functions from both packages in a single program.
Directory Structure
The following directory structure is used in this example.
Understanding the Directory Structure
- test.py is the main Python program.
- com is the main package.
- durgasoft is a sub-package inside the com package.
- module1.py is stored inside the com package.
- module2.py is stored inside the durgasoft sub-package.
- Both packages contain their own
__init__.pyfile.
Step 1: Create __init__.py Files
Create an empty __init__.py file inside both packages.
These files identify the directories as Python packages.
com/__init__.py
com/durgasoft/__init__.py
Explanation
Both __init__.py files are empty.
Their purpose is to identify the directories as Python packages so that modules inside them can be imported.
Step 2: Create module1.py
Create the module1.py file inside the com package.
module1.py
Explanation
The module1.py file defines the f1() function.
This function displays a message indicating that it belongs to the com package.
Step 3: Create module2.py
Create the module2.py file inside the durgasoft sub-package.
module2.py
Explanation
The module2.py file defines the f2() function.
This function displays a message indicating that it belongs to the com.durgasoft sub-package.
Step 4: Create test.py
Create the following test.py program.
This program imports functions from both the main package and the sub-package.
test.py
Requires the package structure to exist on disk (not available in the in-browser editor).
from com.module1 import f1
from com.durgasoft.module2 import f2
f1()
f2()Program Explanation
The program performs the following operations:
- Imports the
f1()function fromcom.module1. - Imports the
f2()function fromcom.durgasoft.module2. - Calls both functions.
- Displays the output generated by both functions.
Output
Output Explanation
After executing the program:
- The
f1()function is executed first. - It prints the message from the com package.
- The
f2()function is executed next. - It prints the message from the com.durgasoft sub-package.
Execution Flow
Important Interview Questions
- What is a nested package?
- Can a Python package contain another package?
- Why does each package require an
__init__.pyfile? - How do you import a function from a sub-package?
- Can modules exist in both the main package and a sub-package?
- How many
__init__.pyfiles are used in this example? - What is the purpose of the durgasoft folder?
Library, Packages, Modules, Functions, Classes, and Variables
In Python, different components are organized in a hierarchical manner.
According to this tutorial, the relationship between a library, packages, modules, functions, classes, and variables is represented as shown below.
Complete Hierarchy
Understanding the Hierarchy
The above hierarchy explains how Python organizes its components from the highest level to the lowest level.
- A Library contains one or more packages.
- A Package contains one or more modules.
- A Module contains functions, classes, and variables.
This hierarchy helps organize Python programs into reusable and manageable components.
Library
A Library is the highest level in the hierarchy.
According to the tutorial:
- A library contains one or more packages.
- Libraries help group related packages together.
- They provide a collection of reusable components for Python applications.
Package
A Package is a collection of related Python modules.
A package is represented by a folder or directory.
- Packages organize related modules.
- Packages improve modularity.
- Packages help resolve naming conflicts.
- A package can also contain sub-packages.
Module
A Module is a Python file that contains reusable program elements.
According to the hierarchy, a module may contain:
- Functions
- Classes
- Variables
Modules make programs easier to maintain and reuse.
Functions
Functions are reusable blocks of code inside a module.
Functions perform specific tasks and can be called whenever required.
Multiple functions can exist inside a single module.
Classes
Classes are templates used for creating objects.
A module may contain one or more classes depending on the application requirements.
Classes help organize data and related operations together.
Variables
Variables are used to store values inside a module.
Modules can contain one or more variables that can be accessed wherever required.
Hierarchy Flow
Component Relationship
| Component | Contains |
|---|---|
| Library | One or more packages |
| Package | One or more modules |
| Module | Functions, Classes, Variables |
| Function | Reusable code |
| Class | Blueprint for objects |
| Variable | Stores data |
Real-World Analogy
| Python Component | Real-World Example |
|---|---|
| Library | University |
| Package | Department |
| Module | Subject |
| Function | Lesson |
| Class | Teacher |
| Variable | Student Record |
Important Interview Questions
- What is the relationship between a library and a package?
- What is the relationship between a package and a module?
- What components can a module contain?
- Which is the highest level in the Python hierarchy?
- Can a library contain multiple packages?
- Can a package contain multiple modules?
- Why is this hierarchy useful in software development?
- Differentiate between a library, package, and module.
Advantages of Using Packages
- Packages organize related modules into a single unit.
- They help resolve naming conflicts.
- They uniquely identify application components.
- They improve the modularity of applications.
- They make large Python projects easier to maintain.
- They encourage code reuse.
Real-World Applications
Packages are widely used in real-world Python development.
- Building large software applications.
- Organizing business applications.
- Developing reusable Python libraries.
- Creating frameworks.
- Managing machine learning and data science projects.
- Developing web applications.
- Structuring enterprise-level software.
Best Practices
- Create meaningful package names.
- Group related modules into the same package.
- Use sub-packages to organize large projects.
- Import only the required members whenever possible.
- Maintain a clear package hierarchy.
- Keep modules focused on a single responsibility.
Common Interview Questions
- What is a Python package?
- What is the purpose of the
__init__.pyfile? - Can a package contain another package?
- What is a sub-package?
- How do you import a module from a package?
- What is the difference between a module and a package?
- What is the difference between a package and a library?
- How do nested packages work?
- Why are packages used in Python?
- What are the advantages of packages?
- What is the hierarchy of Library, Package, Module, Function, Class, and Variable?
- How do you import only a required function from a module?
- A package is a folder that groups related Python modules
- The __init__.py file identifies a directory as a Python package and may be empty
- import package.module imports the complete module
- from package.module import function imports only the required member
- A package can contain sub-packages
- A library contains packages, packages contain modules, and modules contain functions, classes, and variables