Python Modules
Python has an excellent feature of structuring and organizing the codes. These range from statements and control structures through functions, methods, and classes at the scripts level and modules and packages at the libraries level.
What is Python Module?
A module is a file containing a piece of prewritten code that allows a program to perform a function or task without coding it inside the program. Python modules are also saved as ".py" extension
Python has special statements to import a module. Generally, a Python program imports the modules at the beginning of the script. Without importing, a program cannot use a module.
You can create modules or import modules from the PyPI repository using the pip package manager.
Installing 3rd Party Modules in Python
3rd party modules in Python need to be installed before you can import them. You use the pip cli tool to install these 3rd party modules. The basic syntax of the command to install a module is
pip install module_name
For example, to install the numpy module, we enter the following command in the cmd prompt or terminal
pip install numpy
You can also specify a particular version of the module you want to install. Like this
pip install numpy==1.13.6
How to import modules in Python
import statement is used to import a module in a python program. You can import more than modules with the import statement. Like this
import numpy #importing single module
import math, pandas, datetime #importing multiple modules
print(datetime.datetime.now())
print(math.pow(2,2))
Output:
Importing only some parts of a module
Sometimes, you want one part of the module to be imported into our program. For that, you can use the from the statement. With from statement, you can import only the necessary parts of a module instead of the whole module. For example
from math import factorial
print(factorial(5))
Output:
Renaming an Imported Module
as keyword is used in Python to rename an imported module. For example
import datetime as dt
print(dt.datetime.now())
Output:
How to create a module in Python
Creating a module in Python is pretty simple. You have to write the member functions and variables to be included in the module. You then save this code in a .py python file. Then, import the file as you did above using the import statement. For example
You create a file called custom_module_example.py. Then you define a function in the file. Like this
def sayhi():
print("Hi")
You can import this module into our program using the import statement. Like this
import custom_module_example
custom_module_example.sayhi()
Output:
Python dir() function
The dir() function is used to return all the member functions and attributes of a python object or module(even the user-created ones). You use it like this
import custom_module_example
print(dir(custom_module_example))
Output:
Some Popular Python Modules
Given below are some of the most commonly used modules in Python
Module | Description |
---|---|
datetime | Provides functionalities related to handling and representing date and time data |
math | Provides various mathematical functions |
random | Provides functions that can generate data randomly |
pygame | Allows the programmer to create basic games in Python easily |
re | Provides features like pattern matching and regular expression |
numpy | Provides multidimensional array, matrices and various methods to operate on them |
beautifulsoup4 | Provides functions that performs tasks related to web-scraping |
matplotlib | Allows the programmer to plot mathematical graphs |
django | Provides various functionalities related to handling backend of a website |
requests | Provides functions that can send data over http |