Creating Your Own Python Package (Basic) - 15.6 | 15. Python Packages | CBSE Class 10th AI (Artificial Intelleigence)
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Introduction to Creating Packages

Unlock Audio Lesson

0:00
Teacher
Teacher

Hello everyone! Today we're going to learn how to create your own Python package. Can anyone tell me why you might want to create a package in Python?

Student 1
Student 1

To organize my code better?

Teacher
Teacher

Exactly! Creating a package helps in organizing code and reusing functionalities across different projects. So, what do we need to start with?

Student 2
Student 2

Do we need to create a folder?

Teacher
Teacher

Yes! We need to create a folder. Can anyone recall the name of the special file we need to include inside that folder?

Student 3
Student 3

Is it the `__init__.py` file?

Teacher
Teacher

Correct! The `__init__.py` file makes Python recognize the directory as a package. Let's move on to how we can organize our modules within this package.

Structuring Your Python Package

Unlock Audio Lesson

0:00
Teacher
Teacher

Now, let's get into the structure. Our package will have a folder, the `__init__.py` file, and at least one or more modules. What might you name these modules?

Student 4
Student 4

Maybe something like add.py or subtract.py?

Teacher
Teacher

Exactly! Those could contain functions that add or subtract numbers. If you had a module called `add.py`, what do you think would be inside it?

Student 1
Student 1

A function named add() that performs the addition?

Teacher
Teacher

Great thinking! Let’s summarize this part. What do we have so far in creating our package?

Student 2
Student 2

A folder with the `__init__.py` file and our modules for functions!

Using Your Custom Package

Unlock Audio Lesson

0:00
Teacher
Teacher

Great! So once we have our package set up, how do we use it in Python?

Student 3
Student 3

We import it, right?

Teacher
Teacher

Exactly! You can do it like this: `from MyPackage import add`. Now, what will happen if I want to use the `add` function?

Student 4
Student 4

You just call add.add with your numbers!

Teacher
Teacher

Yes! And that makes it super easy to reuse your code across different projects. Who can briefly recap the steps we've learned today?

Student 1
Student 1

Create a folder, add `__init__.py`, include your modules, and then import it!

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section outlines the steps to create your own Python package, emphasizing the folder structure and key components.

Standard

In this section, you will learn the fundamental steps for creating your own Python package, which involves creating a designated folder with an __init__.py file and adding your own modules for better code organization and reusability.

Detailed

Creating Your Own Python Package (Basic)

In this part of the chapter, we delve into the process of creating your very own Python package. The significance of packages lies in their ability to encapsulate functionality and enhance code organization and reusability. The steps to create a package include:

  1. Creating a Folder: Start by creating a new directory that will hold your package. This acts as a container for your modules.
  2. Adding an __init__.py File: Within the newly created folder, include an empty __init__.py file. This file signals to Python that the directory should be treated as a package, enabling you to import it in your scripts.
  3. Including Your Modules: Place your Python modules (files ending in .py) in the package folder, each containing functions or classes you'd like to build upon.

Example Directory Structure

MyPackage/
├── __init__.py
├── add.py  # module containing `add()` function
└── subtract.py  # module containing `subtract()` function

After completing these steps, you can import your functions from your custom package using:

Code Editor - python

Creating your own package not only streamlines code organization but also paves the way for sharing your functionality across multiple projects. This enhances collaboration and reduces code duplication.

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Creating the Package Folder

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

You can also create your own package by:
1. Creating a folder with an init.py file.

Detailed Explanation

To create your own Python package, the first step is to create a dedicated folder. This folder will contain all the modules (Python files) that define the functionalities of your package. Inside this folder, you must include an init.py file. This file is essential because it tells Python that this directory should be treated as a package. Without it, Python won't recognize the contents of the directory as a package, and you will not be able to import anything from it.

Examples & Analogies

Think of your package folder like a toolbox. If the toolbox (folder) is missing, then you can't use the tools (modules) inside it. By adding an init.py file, you are essentially labeling that toolbox as one that belongs to you, making it clear that it holds tools you can use.

Adding Modules to Your Package

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

  1. Adding your Python modules to it.

Example Directory:
mymath/
├── init.py
├── add.py (contains add() function)
├── subtract.py (contains subtract() function)

Detailed Explanation

After creating the folder and the init.py file, the next step is to add your Python modules to this package. A module is simply a Python file that contains functions or classes that you want to group together. In our example, we created a package named 'mymath' with two modules: 'add.py', which includes a function for addition, and 'subtract.py', which includes a function for subtraction. Each module serves a specific purpose, and by organizing them in a package, you can keep related functionalities together.

Examples & Analogies

Imagine you are organizing a library. Each section (module) of your library contains books (functions) related to a specific topic, such as math. By organizing these sections into a single library (package), visitors can easily find all math-related materials in one place, rather than scattered throughout the entire library.

Using Your Custom Package

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

You can now use:
from mymath import add
add.add(3, 5)

Detailed Explanation

Once you have created your package and added your modules, you can easily use it in your Python programs. In the example provided, we demonstrate how to import the 'add' module from the 'mymath' package. By using the 'from' keyword, you can directly access the functions within the 'add' module. In our case, we call the 'add()' function with the parameters 3 and 5, which will return the sum of those two numbers.

Examples & Analogies

Think of this process as using a recipe book. Once you have all your recipes (modules) organized in a single book (package), you can easily refer to it whenever you want to prepare a specific dish. By simply looking up the recipe and following the steps, you can quickly create the dish you desire.

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • Package: A collection of Python modules that are grouped together.

  • Module: A single Python file containing code you can use.

  • Init File: A special file that indicates a directory is a package.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • Creating a package named MyPackage with modules for basic math functions.

  • Importing the add function from the MyPackage and using it to calculate sums.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎵 Rhymes Time

  • To make a package, it's really no trick, create a folder, add __init__.py, pick!

📖 Fascinating Stories

  • Imagine you are organizing your workspace. To keep your tools accessible, you create a toolbox. The toolbox is your package, and the tools inside are your modules!

🧠 Other Memory Gems

  • FIM: Folder, Init file, Modules - the three steps to create a package.

🎯 Super Acronyms

PIM

  • Package = Init file + Modules.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: Package

    Definition:

    A directory containing multiple Python modules and an __init__.py file, allowing the organization of related coding functionality.

  • Term: Module

    Definition:

    A single Python file containing functions, classes, or variables that can be imported and used in other Python files.

  • Term: Init File

    Definition:

    The __init__.py file indicates that the directory should be treated as a Python package.

Call it in a script

from my_package.math_operations import sub
result = sub(5, 3)
- Hint: Think about function definition and imports.