Monday, August 8, 2022

Importing Modules and Objects

When you load Python using the python or python3 command, depending on the operating system, Python loads its core modules, which provide essential functionality. When you need further functionality, you can import one or more additional modules, files containing Python code.

For example, when you need to work with directories, such as creating or deleting them, you can import the os module, which contains methods for interacting with the operating system.

You can either import an entire module by using an import statement or import an individual object from a package by using a from... import statement.

Understanding What Modules Are and Why Python Uses Them

In Python, a module is a stand‐alone file that contains code. Python breaks down code into modules so as to have multiple smaller files rather than one gargantuan file. These smaller files have various advantages, such as helping the organization of code by functionality, streamlining the updating of code, and making code run better on less‐powerful systems by avoiding loading items that are not needed.

The main disadvantage of having code in separate modules is that your code must load any modules it needs. But as you will see, loading the modules is quick and easy.

Import a Module

To import a module, use the import keyword and specify the module name. For example, the following statement imports Python’s os module, which provides operating system–related commands:

import os

Similarly, if you have created a custom module named acme_calculations.py, you can import it by using the following command:

import acme_calculations

Note that you omit the .py file extension from the custom module’s filename in the import statement. When you import a module of your own like this, navigate to the directory that contains the module first, and then launch Python from there. Alternatively, you can import the module from a subdirectory of the directory from which you launched Python. For example, if the acme_calculations module is stored in the final subdirectory, specify the subdirectory like this:

from final import acme_calculations

Access the Contents of an Imported Module

When you import a module like this, you specify the module’s name to access its contents. For example, the os module’s contents include the path module, which provides methods for working with file‐system path names. After importing the os module, you access the path module like this:

os.path

Similarly, if you have imported the acme_calculations module, and it contains a method named ave_product, you access it through the module like this:

acme_calculations.ave_product()

Import an Object from a Module

Instead of importing an entire module, you can import a single object from a module. You might do this if that object is the only part of the module you will need and you want to be able to refer directly to the object rather than having to refer to it via the module. Counterintuitively, importing only an object does not reduce resource usage, as Python imports the whole module into its mapping table; the difference is in how you refer to the object.

To import an object from a module, begin the statement with the from keyword; then supply the module name, then the import keyword, and finally the object name. For example, the following statement imports the path module from the os module:

from os import path

After importing a single object like this, you refer to it by its unqualified name, such as path in this case, rather than via its parent module, such as os.path. Here is an example:

print(path)

If the object you import contains other objects or methods, you can access those objects or methods by using the name of the imported object followed by a period and the name of the item you want to use. For example, the path object contains many methods, including os.path.basename(), which returns the base name of the specified path. After importing the path object, you can access the basename() method via the path object like this:

path.basename()

You can also import a nested object on its own. For example, the following statement imports basename() from os.path:

from os.path import basename

Share:

0 comments:

Post a Comment