Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Listen to a student-teacher conversation explaining the topic in a relatable way.
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?
To organize my code better?
Exactly! Creating a package helps in organizing code and reusing functionalities across different projects. So, what do we need to start with?
Do we need to create a folder?
Yes! We need to create a folder. Can anyone recall the name of the special file we need to include inside that folder?
Is it the `__init__.py` file?
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.
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?
Maybe something like add.py or subtract.py?
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?
A function named add() that performs the addition?
Great thinking! Let’s summarize this part. What do we have so far in creating our package?
A folder with the `__init__.py` file and our modules for functions!
Great! So once we have our package set up, how do we use it in Python?
We import it, right?
Exactly! You can do it like this: `from MyPackage import add`. Now, what will happen if I want to use the `add` function?
You just call add.add with your numbers!
Yes! And that makes it super easy to reuse your code across different projects. Who can briefly recap the steps we've learned today?
Create a folder, add `__init__.py`, include your modules, and then import it!
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
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.
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:
__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.
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:
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.
Dive deep into the subject with an immersive audiobook experience.
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.
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.
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.
Signup and Enroll to the course for listening the Audio Book
Example Directory:
mymath/
├── init.py
├── add.py (contains add() function)
├── subtract.py (contains subtract() function)
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.
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.
Signup and Enroll to the course for listening the Audio Book
You can now use:
from mymath import add
add.add(3, 5)
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.
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.
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.
See how the concepts apply in real-world scenarios to understand their practical implications.
Creating a package named MyPackage with modules for basic math functions.
Importing the add function from the MyPackage and using it to calculate sums.
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
To make a package, it's really no trick, create a folder, add __init__.py
, pick!
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!
FIM: Folder, Init file, Modules - the three steps to create a package.
Review key concepts with flashcards.
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.
from my_package.math_operations import sub
result = sub(5, 3)
- Hint: Think about function definition and imports.