Importing Modules From Subpackages
00:00
A package is just a folder containing one or more Python modules, one of which must be __init__.py
. So it’s entirely possible to have the following package structure.
00:11
You have a mypackage/
folder with the three files __init__.py
, module1.py
, and module2.py
. Additionally, you have a subdirectory named mysubpackage/
inside the mypackage/
folder.
00:26
And inside the mysubpackage/
folder is another __init__.py
file and a file named module3.py
. A package nested inside another package is called a subpackage. In this example, the mysubpackage/
folder is a subpackage of mypackage/
because it contains an __init__.py
module as well as a second module called module3.py
. Let’s create the structure to see how it works.
00:56
Using your computer’s file explorer or some other tool, create the mysubpackage/
folder on your computer. Make sure you place the folder inside the mypackage/
folder you created earlier.
01:10
In IDLE, open a new editor window and create an __init__.py
file that you save in mysubpackage/
.
01:24
Then open another file that you save as module3.py
, and save it next to the __init__.py
in the mypackage/
folder.
01:35
So the place where you store those files is very important for this. In your module3.py
file, add the following code. Define a variable named people
that equals a list with four strings: ["Christal", "Tappanita, "Martina," "Kate"]
.
01:59
Save the file and then open the main __init__.py
file that’s in the root directory of the package_examples/
. That’s the project folder.
02:07
Remove any existing code that is in the main.py
file at this moment, and replace it with this code: from mypackage.module1 import greet
, and in the next line, from mypackage.mysubpackage.module3
import people
. An empty line, and then you loop through the list. for person in people
: and on the next line, indented, greet(person)
.
02:44
The people
list from the module3
module inside mysubpackage
is imported via the dotted module name mypackage.mysubpackage.module3
. Now save and run main.py
.
02:59
Cool. The output in the interactive window is Hello, Christal!
Hello, Tappanita!
Hello,
Martina!
and Hello, Kate!
. And that’s how you can work with subpackages in Python.
03:11 Subpackages are great for organizing code inside very large packages. They help keep the folder structure of a package clean and organized. However, deeply nested subpackages introduce long dotted module names.
03:25 You can imagine how much typing it would take to import a module from a subpackage of a subpackage of a subpackage of a package. So it’s good practice to keep your subpackages at most one or two levels deep.
03:37 And it’s also a good practice to keep video courses on the shorter side, which is a great coincidence because let me look at my notes. There is not that much that I wanted to tell you today about modules and packages, but before we wrap up this course, I have a special treat for you in the next lesson.
Become a Member to join the conversation.