Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several methods to solve the problem. But, one method will be the best one, and it will be based on the time to solve, simplicity, and accuracy. While programming, programmers also need such solutions. When a programmer first tries to solve a problem, he uses all the methods and analyzes the best method considering many factors and scenarios. The program can be lengthy. In Python, all such programs that a typical Python-programmer will need while coding is stored in modules. So, a module is a file with a set of functions, classes, variables, and statements with logic that we can include in our programs. The programmer need not write the whole code, instead he can include the already developed modules and can use the functionality.
- Related logics are grouped simultaneously and placed into a file called the modules. Hence, every module consists of similar codes. This improves the organization of a code when we use the modules in our program.
- To use the logic specified inside a module in our code, we need to import the module using the import statement for the interpreter to recognize and implement the logic.
- When we import a module into our program, the whole module will be imported. To directly import a function inside the module, we can use the syntax:
import module_name. function_name
from module_name import function_name
- If we want to import all the functions, variables, and constants of a module, we can use an asterisk:
from module_name import *
- If the module specified by the user does not exist, the interpreter raises an error
ImportError: No module named “module_name."
- We can even create custom modules by importing the file in which the function or the logic we need is written into any program.
This is the basic idea of modules. Now, let us see the list of modules a programmer will need in his programming journey one or the other time. There are about 200 modules in Python, varying with the distribution. These modules are organized in alphabetical order on the official website - here.
These are some frequently used modules and libraries in Python:
|It provides us with several multi-dimensional array processing functions.
|The best library of modules with data analysis, data science, and machine learning solutions.
|Contains data visualization and plotting functions and is an extension to the numpy library.
|It is based on matplotlib. Also used for plotting graphs to visualize the vast amount of data.
|Best library for machine learning applications. Contains a lot of tools for statistics and ml.
Modules vs. Libraries vs. Packages:
When searching for modules, we might run into many libraries and packages. These three words are used synonymously sometimes. But, these are not the same words.
|A simple Python file ends with a .py extension.
|Collection of already compiled programs to execute in our codes.
|Collection of similar modules in one place.
A module is a base here. These are normal Python files that contain some code. We save the file and use them whenever we want in our programs. Based on the type and the uses of the code, modules are stored in packages.
Packages and libraries are more exchangeable words. A package can have sub-packages in it. In a package, there will be a __init__. py file defines it as a package, and the interpreter recognizes it as a package.
Like the generic term, a library is the collection of books and resources; in Python, a library is the collection of already made codes, documentation, classes, values, etc. The Python standard library contains the syntax and semantics of the tokens we use in Python, like if-else, loops, etc. In-built modules, that contain the basic functionality of Python are stored in the Python standard library. Python libraries are generally written in the C programming language.