Working With Packages
00:00
To see how importing a module from a package works, create another IDLE editor window. And there you will create your calling module. At the beginning of the file, you import the package with import mypackage
.
00:17
Before adding any more code, just save this file for now. Name it main.py
, and now it’s important you save it in your project folder. That means main.py
is not in the same folder as __init__
, module1
, and module2
, but in the parent folder that you named packages_example/
.
00:41
Let’s see what happens when you run main.py
. Oh, hello there. No error at all. That’s a new one. That means importing your package works. Let’s continue with main.py
, and as a little heads up, with the code changes you’re adding now, we’ll get an error soon.
00:58
We’ll get to it, and I will explain why. But for now, in your main.py
file, add the following code underneath import mypackage
, add a new line that says mypackage.module1.greet("Cedar")
.
01:22
And then in the next line, mypackage.module2.depart()
and then you pass in the string "boredom"
.
01:34
Save main.py
and run the module. And as promised, there is an error. But have a closer look. This time, it’s not a NameError
like we had in the former lessons. In the interactive window, An AttributeError
is raised. AttributeError: module 'mypackage' has no attribute 'module1'
.
01:58
The reason for this error is that you successfully imported mypackage
and then Python looks for the attributes module1
and module2
of mypackage
.
02:09
Python doesn’t know at this point that it should actually look for modules. That’s why you don’t get a NameError
, but an AttributeError
.
02:19
So when you import the mypackage
module, the module1
and module2
namespaces aren’t imported automatically, so you need to import them too. And for this, you adjust the import
statement at the beginning and use two new import
statements instead.
02:39
The first import
statement is import mypackage.module1
, and on the next line, import mypackage.module2
. The rest of the code stays unchanged.
02:53
But now you should have those two import
statements at the beginning of the file, and the import mypackage
import statement from before is not there anymore.
03:04
Now when you save and run main.py
, both greet()
and depart()
get called, and in the interactive shell, you can see Hello, Cedar!
Goodbye, boredom!
Let’s recap what happens here.
03:14
Under the hood, modules are imported from packages using dotted module names with the following format: import <package_name>.<module_name>
.
03:25
So you type the name of the package, followed by a dot, and then the name of the module you want to import. In this case, for both import
statements, the package name is mypackage
, and then you add a dot, and after the dot, module1
in the first line, and in the second line, you use module2
as the module name after the dot.
03:46
Since the folder name is the package name that you use in the import
statement, the name of the package folder must be a valid Python identifier.
03:55 That means the folder name may contain only uppercase and lowercase letters, numbers, and underscores, and they must not start with a digit.
04:06
As with modules, there are several variations on the import
statement that you can use when importing packages. Let’s have a look at them next.
alphafox28js on Dec. 5, 2023
This would have definitely been beneficial in the OOP Segment when building a farm. I think this may resolve the 2% of unusable code I have.
Might I suggest adjusting the course structure as to where Modules & Packages are taught before OOP?
Become a Member to join the conversation.
adityahpatel on Nov. 17, 2023
Working with packages Timestamp 2:57 Why is Python looking for attributes and not modules, by default? Why do we need to explicitly do
import mypackage.module1
orfrom mypackage import module1
?