You can import individual modules and packages in a couple of ways, depending on how they will be used in your program.
Importing by full name
If you'll be using many parts of a complex package, use the following format to import an individual item by its full name:
This type of package import statement has a couple of rules:
• Each name except for the last must be a package.
• The last name must be a module or a package; it can't be a class, function, or named object (attribute).
TECHNICAL To call a function from a module imported by its full name, type the STUFF full name of the module and then the function name, like this:
Import by using from
If you'll be using only a few parts of a package, use the following format to import an individual item by its full name:
from graphics.filters.noise import edges
Tip This format works for importing any of the following objects defined in the package: submodule, subpackage, function, class, and named object.
Here are a couple of examples of importing an individual item:
• The following example imports the noise module:
• from graphics.filters import noise
If you import noise this way, you use this code to call its function:
• The following example imports the edges() function:
• from graphics.filters.noise import edges
If you import edges() this way, you use this code to call it:
Was this article helpful?