Introducing Packages
00:00 Modules allow you to divide a program into individual files that can be reused as needed. Related code can be organized into a single module and kept separate from other code. Packages take this organizational structure one step further.
00:17
In Python, a package is basically a folder that contains one or more Python modules. On the slide, you can see an example of a package. The structure looks like this. There is a folder named mypackage/
, and in the folder there are three Python files. One is named module1.py
, and another is named module2.py
.
00:37
And there is also a Python file that looks a bit different. There is a file named __init__.py
, or short __init__.py
.
00:50
To make a folder a regular Python package, the folder must contain an __init__
module. The __init__.py
module doesn’t need to contain any code. It only needs to exist so that Python recognizes the mypackage/
folder as a Python package.
01:11 With this package structure in mind, let’s create your first package.
Become a Member to join the conversation.