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

The Math Module
Provides functions used to perform mathematical operations.
>>> import math
>>> math.pow(5,2)
25
Documentation: https://docs.python.org/3/library/math.html
The Random Module
Documentation: https://docs.python.org/3/library/random.html
The Datetime Module
Documentation: https://docs.python.org/3/library/datetime.html
The Requests Module
Requests is an elegant and simple HTTP library for Python
>>> import requests
>>> response = requests.get("https://fabiopacifici.com")
>>> print(response.text)
Documentation: https://requests.readthedocs.io/en/master/user/quickstart
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
Documentation: https://docs.python.org/3/library/os.path.html#module-os.path
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('/')
print(diskUsage)
Documentation: https://docs.python.org/3/library/shutil.html
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)
2.0
Documentation: https://psutil.readthedocs.io/en/latest/
PIL Module
Documentation: https://pypi.org/project/PIL/
Alternative fork: https://pypi.org/project/Pillow/
PANDAS Module
# https://pypi.org/project/pandas/# https://pandas.pydata.org/docs/
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']
Documentation: https://docs.python.org/3/library/csv.html
The RE Module
This module provides regular expression matching operations.
Documentation: https://docs.python.org/3/library/re.html
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 params.py
import sys
print(sys.argv)
Then executing the file in the terminal:
$ ./params.py one two three
['./params.py', 'one', 'two', 'three']
The result is a list containing our filename first followed by any parameter we passed to it.
Documentation: https://docs.python.org/3/library/sys.html
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
>>> subprocess.run(["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 orders.py
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 app.py
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.
Documentation: https://docs.python.org/3.7/library/subprocess
Unittest Module
Documentation: https://docs.python.org/3/library/unittest.htm
Glob Module
Documentation: https://docs.python.org/3/library/glob.html