Python Modules: Import, Using Modules

What is Module?

The module is used to arrange the Python program logically. Basically module is a file that includes a set of functions and logical coding. A runnable code is also a part of modules. The programmer can use any source file in Python as a module. In Python, different types of built-in modules also present and a user-defined module can also be created as per requirement. There are lots of built-in modules are available in the python library and the programmer can use them whenever they want to use it. These built-in modules are used in a program to improve the coding part and reduce the length of code.

We will see how to use built-in modules below,

The import keyword:

The import keyword is used to use any source file as a module in Python application. There is a search path present internally. When the interpreter starts its operation and loads the import module, it starts searching for that module in the search path. If the interpreter becomes able to search the module in the search path, it will import that module in the application. A search path is nothing but a list of directories.

Syntax:

import module_name

Using the statement:

There is another way to import modules using the statement. The fro statement is used to import particular attributes of a module. The entire module file cannot be loaded using this from the statement. Only important attributes can be imported.

Syntax:

from module_name import attributes

Import all attributes from a module:

In this way, all the attribute's names are imported from a specific module. This is one of the easiest ways to import all the attributes.

Syntax:

from module_name import *

User-defined module:

To create a user-defined module, at first, a python file should be created. This file should have the extension “.py”. The user-defined module can be easily imported in the current application by using the import statement. Function and variables can be used in the user-defined module. The way of creation and use of the user-defined module is described in the below:

1. Creation of user-defined module.

# Sample Code
def function_name(parameter_name):
     print("Sample text1" + parameter_name)

2. Save the file using the extension “.py”. Like, firstmodule.py

3. Use of the module

# Sample code
import firstmodule
firstmodule.function_name("Sample text2")

Scoping or locating of modules:

When a module is present in an application using the import statement, the interpreter starts searching in the search path for that module name. There is a sequence in which the interpreter starts searching. The sequence is:

  1. The interpreter first starts its search from the currently executing directory.
  2. If the interpreter could not find the module in the current directory, it will search each and every directory present in the shell variable Python PATH. The PATH variable is nothing but the environment variable. It includes a list of currently working directories.
  3. If the interpreter could not find the module after using the above two mentioned ways, it will start searching in the default path. Usually, the default path of Python is the python directory under the library file.