Author: - Post Category: Python - Date:April 20, 2020

15 Python Modules You should know

Fifteen Python modules you should know is a collection most of the modules covered in my Python course.

Categories: Python. Tags: modules and python.
Python modules

The Math Module

Provides functions used to perform mathematical operations.

>>> import math
>>> math.pow(5,2)


The Random Module


The Datetime Module


The Requests Module

Requests is an elegant and simple HTTP library for Python

>>> import requests
>>> response = requests.get("")
>>> print(response.text)


The OS Module

This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.

>>> import os
>>> print(os.getcwd())

The OS.Path Sub-module:

This module implements some useful functions on pathnames .

>>> import os
>>> os.path.exists('path-here')

The Environment Variable

Like in a linux prompt we can check environment variables ($PATH) we can use the get() method of the environ dictionary for the os module to do the same.

>>> import os
>>> print("HOME PATH:", os.environ.get("HOME", "Not found"))      
HOME PATH: /home/fab

>>> print("PYTHON PATH:", os.environ.get("PYTHON", "Not found"))  
PYTHON PATH: Not found

When a given path name doesn’t exist the second parameter of the get() method let’s us return a value that we want to display.

Note: to add a given value to a none existent variable in the current environment we can add it using the export=value via the command line

$ export PYTHON=/usr/bin/env/python3


The SHUTIL Module

The shutil module offers a number of high-level operations on files and collections of files.

import shutil
diskUsage = shutil.disk_usage('/')


The Psutil Module

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python

>>> import psutil
>>> psutil.cpu_percent(interval=1)


PIL Module


Alternative fork:



The CSV Module

The csv module implements classes to read and write tabular data in CSV format.

>>> import csv
>>> with open('employees.csv') as file:
...     read_file = csv.reader(file)
...     for row in read_file:
...             print(row)
['Bailey Thomas', ' baileyt', ' Human Resources']
['Blake Sousa', ' sousa', ' IT infrastructure']
['Cameron Nguyen', ' nguyen', ' Marketing']
['Charlie Grey', ' greyc', ' Development']
['Chris Black', ' chrisb', ' User Experience Research']
['Courtney Silva', ' silva', ' IT infrastructure']


The RE Module

This module provides regular expression matching operations.


The Sys Module

This module provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter.

Using the argv command line arguments dynamic object:

Creating a file like

import sys

Then executing the file in the terminal:

$ ./ one two three
['./', 'one', 'two', 'three']

The result is a list containing our filename first followed by any parameter we passed to it.


SubProcess Module

This module allows you to spawn processes, connect to their input/output/error pipes, and obtain their return codes.

the run() function runs a command, waits for it to complete, then returns a
CompletedProcess instance.

>>> import subprocess
>>>["ls", "-alt"])
total 36
drwxrwxrwx 1 fab fab  4096 Mar 28 17:56 The_Python_Tutorial       
drwxrwxrwx 1 fab fab  4096 Mar 23 23:17 Google_IT_Python_certification
-rwxrwxrwx 1 fab fab    65 Mar 23 20:01 sample.txt
drwxrwxrwx 1 fab fab  4096 Mar 23 20:01 .
drwxrwxrwx 1 fab fab  4096 Mar 23 18:14 testDir
-rwxrwxrwx 1 fab fab 10232 Mar 22 15:39 fabioesere.jpg
-rwxrwxrwx 1 fab fab 22837 Mar 21 22:34
drwxrwxrwx 1 fab fab  4096 Mar 15 16:06 practice
drwxrwxrwx 1 fab fab  4096 Mar 12 18:54 ..
-rwxrwxrwx 1 fab fab    24 Mar  5 17:44
drwxrwxrwx 1 fab fab  4096 Mar  3 23:36 Pyton_OS_Module
drwxrwxrwx 1 fab fab  4096 Feb 16 19:13 learning_Python
drwxrwxrwx 1 fab fab  4096 Feb 13 18:44 .vscode
CompletedProcess(args=['ls', '-alt'], returncode=0)

The run command accepts a list where the first element is the command and the following are the options and or arguments of the command.


Unittest Module


Glob Module