Nearby lessons

70 of 108

Python - Packages

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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__.py file.
  • Each sub-package contains Python modules.

Advantages of Packages

According to the tutorial, the main advantages of packages are:

  1. We can resolve naming conflicts.
  2. We can identify our components uniquely.
  3. It improves the modularity of the application.

Package Hierarchy

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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__.py file.

Summary

Topic Description
Package Collection of related Python modules.
Package Location Folder or directory.
__init__.py Identifies a folder as a Python package.
Sub-package Package inside another package.
Main Advantages Resolves naming conflicts, uniquely identifies components, and improves modularity.

Important Notes

  1. A package is an encapsulation mechanism used to group related modules into a single unit.
  2. A package is represented by a folder or directory.
  3. Every Python package contains an __init__.py file.
  4. The __init__.py file can be empty.
  5. A package can contain one or more sub-packages.
  6. Each sub-package also contains its own __init__.py file.
  7. According to the tutorial, packages help:
    • Resolve naming conflicts.
    • Identify components uniquely.
    • Improve the modularity of applications.

Quick Recap

  • A package is a collection of related Python modules.
  • A package is represented by a folder.
  • The __init__.py file identifies a folder as a Python package.
  • Packages help organize large Python applications.
  • A package can contain one or more sub-packages.

Important Interview Questions

  1. What is a Python package?
  2. How is a package different from a module?
  3. Why do we use packages in Python?
  4. What is the purpose of the __init__.py file?
  5. Can the __init__.py file be empty?
  6. What is a sub-package?
  7. What are the advantages of using packages?
  8. 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__.py file.
  • 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.

Directory Structure

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Understanding the Directory Structure

  • test.py is the main Python program.
  • pack1 is the Python package.
  • __init__.py identifies pack1 as 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Program Explanation

The statement:

import pack1.module1

imports 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Quick Recap

  • pack1 is the package.
  • module1.py is the module.
  • f1() is the function inside the module.
  • The entire module is imported using the import statement.
  • The function is accessed using the complete package path.

Important Interview Questions

  1. How do you create a Python package?
  2. Why is the __init__.py file required?
  3. How do you import an entire module from a package?
  4. How do you call a function after importing the complete module?
  5. What is the purpose of the statement import pack1.module1?
  6. Why do we write pack1.module1.f1() instead of simply f1()?

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Understanding the Import Statement

The statement:

from pack1.module1 import f1

imports 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.

Key Points

  • pack1 is the package.
  • module1.py is the module.
  • f1() is the function inside the module.
  • __init__.py is an empty file that makes pack1 a Python package.
  • Version 1 imports the complete module.
  • Version 2 imports only the required function.

Execution Flow (Version 2)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Quick Recap

  • Python allows importing only the required members from a module.
  • The from ... import statement provides direct access to imported members.
  • No package or module name is required while calling the imported function.
  • This makes the code shorter and easier to understand.

Best Practice

Choose the import style based on your requirements:

  • Use import package.module when many functions or variables from the module are needed.
  • Use from package.module import function when only a few members are required.

Important Interview Questions

  1. What is the difference between import package.module and from package.module import function?
  2. When should we import the complete module?
  3. When should we import only the required function?
  4. Why can f1() be called directly in Version 2?
  5. Which import statement produces shorter code?
  6. Which version provides direct access to imported functions?
  7. Can both import styles produce the same output?
  8. 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.

Directory Structure

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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__.py file.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

com/durgasoft/__init__.py

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Program Explanation

The program performs the following operations:

  1. Imports the f1() function from com.module1.
  2. Imports the f2() function from com.durgasoft.module2.
  3. Calls both functions.
  4. 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Key Points

  • com is the main package.
  • durgasoft is a sub-package.
  • Each package contains its own __init__.py file.
  • Modules can exist in both the main package and its sub-packages.
  • Functions from both packages can be imported into a single Python program.

Quick Recap

  • A package can contain another package.
  • Such packages are called nested packages or sub-packages.
  • Python supports importing modules from multiple package levels.
  • Both the main package and sub-package require an __init__.py file.

Important Interview Questions

  1. What is a nested package?
  2. Can a Python package contain another package?
  3. Why does each package require an __init__.py file?
  4. How do you import a function from a sub-package?
  5. Can modules exist in both the main package and a sub-package?
  6. How many __init__.py files are used in this example?
  7. 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

Key Points

  • A library is the highest level in the hierarchy.
  • A library contains one or more packages.
  • A package contains one or more modules.
  • A module contains functions, classes, and variables.
  • Each level helps organize Python programs into smaller, reusable components.

Quick Recap

  • Library → Collection of packages.
  • Package → Collection of modules.
  • Module → Collection of functions, classes, and variables.
  • This hierarchy improves code organization and reusability.

Important Interview Questions

  1. What is the relationship between a library and a package?
  2. What is the relationship between a package and a module?
  3. What components can a module contain?
  4. Which is the highest level in the Python hierarchy?
  5. Can a library contain multiple packages?
  6. Can a package contain multiple modules?
  7. Why is this hierarchy useful in software development?
  8. Differentiate between a library, package, and module.

Complete Chapter Summary

This chapter introduced the concept of Python packages and explained how packages help organize related modules in a structured manner.

It also demonstrated creating packages, working with sub-packages, importing modules and functions, and understanding the relationship between libraries, packages, modules, functions, classes, and variables.

Topic Description
Package Collection of related modules.
__init__.py Identifies a directory as a Python package.
Sub-package Package inside another package.
Package Import Import modules from a package.
Nested Package Package inside another package.
Library Collection of one or more packages.
Module Collection of functions, classes, and variables.

Important Notes

  1. A package is a collection of related Python modules.
  2. Every package must contain an __init__.py file. The file can be empty.
  3. A package can contain one or more sub-packages.
  4. Modules inside packages can be imported by using the complete package path.
  5. We can directly import only the required functions from a package module.
  6. A library contains one or more packages.
  7. A package contains one or more modules.
  8. A module contains functions, classes, and variables.

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.

Key Concepts Covered

Concept Purpose
Package Groups related modules.
Module Stores reusable code.
Sub-package Organizes packages into multiple levels.
__init__.py Marks a directory as a package.
Import Statement Accesses modules and functions.
Nested Packages Improve project organization.

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

  1. What is a Python package?
  2. What is the purpose of the __init__.py file?
  3. Can a package contain another package?
  4. What is a sub-package?
  5. How do you import a module from a package?
  6. What is the difference between a module and a package?
  7. What is the difference between a package and a library?
  8. How do nested packages work?
  9. Why are packages used in Python?
  10. What are the advantages of packages?
  11. What is the hierarchy of Library, Package, Module, Function, Class, and Variable?
  12. How do you import only a required function from a module?

🧠 Test Your Knowledge

48 Questions

Progress: 0 / 48
Keep Going!Python - Exception Handling