Summarizing Import Statements for Packages
00:00 As a rule of thumb, the same guidelines for importing names from modules apply to importing modules from packages. So you should stick with keeping your imports as explicit as possible so that the modules and names imported into the calling module have the appropriate context.
00:18
So this would be import <package>.<module>
, just like you did in the IDLE session, and then you can access names with <package>.<module>.<name>
in your code.
00:31
This way when you encounter names from the imported module, there’s no question where those names come from, but sometimes package and module names are long, and you find yourself typing something like <package>.<module>
over and over again in your code.
00:47
Then you could use the from <package> import <module>
format, which allows you to skip the package name and import just the module name into the calling module’s namespace. With an import
statement like this, you can just type <module>.<name>
to access a name from the module.
01:03 While this no longer tells you which package the name comes from, it does keep the context of the module apparent.
01:11
If you want to import multiple modules from one package, then you can list them in one line saying, from mypackage import module1, module2
, and later then in your code, you would call module1
and module2
directly without the mypackage
part for it.
01:31 So now that you’ve learned about packages, let’s go a level deeper and I’ll tell you something about subpackages in the next lesson.
Become a Member to join the conversation.