Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
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!
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.
PIM: Package = Init file + Modules.
Term
What is a Python package?
Click to flip
Term
What does the `__init__.py` file do?
Click to flip
A directory containing multiple Python modules and an __init__.py file, allowing the organization of related coding functionality.
A single Python file containing functions, classes, or variables that can be imported and used in other Python files.
The __init__.py file indicates that the directory should be treated as a Python package.
This section outlines the steps to create your own Python package, emphasizing the folder structure and key components.
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:
Creating a Folder: Start by creating a new directory that will hold your package. This acts as a container for your modules.
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.
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.
MyPackage/
├── __init__.py
├── add.py # module containing `add()` function
└── subtract.py # module containing `subtract()` functionAfter completing these steps, you can import your functions from your custom package using:
from MyPackage import add
add.add(3, 5) # Where add() is defined in add.pyCreating 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.