Modules in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning

Overview

Modules in Python are the python files containing definitions and statements of the same. It contains Python Functions, Classes or Variables.Modules in Python are saved with the extension .py.

What are Modules in Python?

Consider a bookstore. In the bookstore, there are a lot of sections like fiction, physics, finance, language, and a lot more, and in every section, there are over a hundred books.

In the above example, consider the book store as a folder, the section as python modules or files, and the books in each section as python functions, classes, or python variables.

Formal Definition of Python Modules`

Modules in Python can be defined as a Python file that contains Python definitions and statements. It contains Python code along with Python functions, classes, and Python variables. The Python modules are files with the .py extension.

Features of Python Modules

  • Modules provide us the flexibility to organize the code logically. Modules help break down large programs into small manageable, organized files.
  • It provides reusability of code.

Example of Modules in Python

Let's create a small module.

  • Create a file named example_.py. The file's name is example_.py, but the module's name is example_.
  • Now, in the example_.py file, we will create a function called print_name().

Code(example_.py):

  • Now, we have created Python modules example_.
  • Now, create a second file at the same location where we have created our example_ module. Let's name that file as test_.py.

Code(test_.py):

Output:

Types of Python Modules

There are two types of Python modules:

  1. InBuilt modules in Python
  2. User-Defined Modules in Python

InBuilt Module

There are several modules built-in modules available in Python.

Built-In modules like:

  • math
  • sys
  • os
  • random and many more.

Calling an Inbuilt Module

Code:

Output:

random.randint() function will generate a new number whenever the function is called. It will generate a new number in the given range. i.e., [0, 20)

Variables in Python Modules

We have already discussed that modules contain functions and classes. But apart from functions and classes, modules in Python can also contain variables in them. Variables like tuples, lists, dictionaries, objects, etc.

Example:

Let's create one more module variables, and save the .py as variables.py

Code:

Create another Python file, and use the variables module to print the factorial of a number, print the power of 6, and what is the second alphabet in the alphabet_list.

Code:

Output:

What is an Import Statement in Python?

The import statement is used to import all the functionality of one module to another. Here, we must notice that we can use the functionality of any Python source file by importing that file as the module into another Python source file.

We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times it has been imported into our file.

How to Import Modules in Python

For importing modules in Python, we need an import statement to import the modules in a Python file.

Code:

Output:

Using from <module_name> import <class/function/variable_name> statement

When we are using the import statement to import either built-in modules or user-defined modules, that states we are importing all the functions/classes/variables available in that modules. But if we want to import a specific function/class/variable of that module.

We can import that specific function/class/variable, by using from <module_name> import <class/function/variable_name> statement then we can import that specific resource of that module.

Syntax:

The syntax of from <module_name> import statement is:

Example:

Let's see an example of from <module_name> import <class/function/variable_name> statement:

Code:

Output:

Tip:

If we use '*' in place of function_name/class_name, it will import all the resources available in that module.

Example:

Let's see an example of the same:

Code:

Output:

Python Modules Search Path

In the previous sections, we have seen how to import the modules using the import statement. Now we will see how Python searches for the module.

In Python, when we use the import statement to import the modules. The Python interpreter looks at the different locations.

After using the import statement to import modules, firstly, the Python interpreter will check for the built-in modules. If the module is not found, then the Python interpreter will look for the directories defined in the path sys. path.

The order of search is in the following order:

  • At first, the interpreter looks in the current working directory.
  • If the module is not found in the current working directory, then the interpreter will search each directory in the PYTHONPATH environmental variable.
  • If now also the module is not found, then it will search in the default installation directory.

Renaming a Module

Python provides an easy and flexible way to rename our module with a specific name and then use that name to call our module resources. For renaming a module, we use the as keyword. The syntax is as follows:

Code:

Output:

Reloading a Module

We can import a module only once in a session.

Suppose you are using two modules, variables_ and example_ simultaneously in a Python file, and the variables_ module is updated while you are using these modules. You want the updated code of that module, which is updated. Now that we know, we can import a module in our session only once.

So, we will use the reload function available in the imp module to get the updated version of our module.

The dir() built-in Function

The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the classes, variables, and functions that are defined in a module.

Output:

Conclusion

  • In this article, we have learned about modules in Python. There are 2 modules, i.e., Built-In and user-defined modules.
  • We can store functions, classes, and variables in our module.
  • Next, we have seen how to import our module. For importing a module, we will use the import statement, and if we want to use a specific function of that module, we will use the from <module_name> import statement.
  • Next, we have seen how to rename the modules and the use of the dir() function.

See Also: