Python Modules

What are Python modules?

In Python, modules are libraries that contain additional functions. To use them, you need to import the module into your Python source code.

What are they used for?

Each module is designed to solve specific practical problems and includes instructions, commands, and functions to make programming easier in a particular domain.

For example, functions for managing emails, calculating matrices, processing data, performing mathematical or scientific calculations, and more.

What Python modules are available?

There are thousands of modules available in Python.

Here are some practical examples:

  • bz2 handles opening, creating, and modifying bz2 compressed files.
  • ftplib is used for transferring files via FTP.
  • gzip handles opening, creating, and modifying gz compressed files.
  • math provides mathematical and trigonometric functions (sine, cosine, etc.).
  • matplotlib offers functions for creating plots and charts.
  • multiprocessing enables parallel processing across multiple CPU cores.
  • networkx is used for working with simple, multiple, and directed graphs.
  • numpy provides functions for scientific computing.
  • nltk is used for natural language processing.
  • os allows for operating system interaction.
  • pandas is used for data manipulation and analysis.
  • pyautogui automates GUI interactions.
  • pygame provides functionalities for creating interactive graphics and games.
  • pytorch is useful for tensor computation and deep learning.
  • platform gathers system information, such as Python version and OS details.
  • random generates random numbers.
  • scikit-learn is dedicated to machine learning.
  • seaborn is a data visualization library, particularly for statistical graphics.
  • shelve saves and retrieves Python objects to and from a file.
  • smtplib is used for sending email messages.
  • sympy performs symbolic mathematics.
  • sys provides system-specific parameters and functions.
  • tarfile manages tar archive files.
  • threading enables multi-threaded programming.
  • torch is used for building neural networks and machine learning models.
  • urllib reads the content of web pages.
  • zipfile manages zip archive files.

How to import modules in a script

To use the functions of a module, you need to import it into your script with the import statement.

import module_name

After importing the module into your script or console, you can use its functions.

module_name.function_name()

To call a function, write the module name followed by the function name, separated by a dot.

Example

Trigonometric functions are not available by default in Python.

To use them, you need to import the math library.

 

  1. import math
  2. x = 1
  3. math.sin(x)

In the first line, we import the math module. It is a library that includes basic mathematical functions.

In the third line, we call the math.sin(x) function to calculate the sine of x.

What if the module is not imported?

Some modules are already included in Python (e.g., math) and can be imported directly into the script.

>>> import math
>>>

Other modules, however, need to be installed first (e.g., pandas, nltk, etc.) in the Python interpreter before they can be imported into a script.

If you try to import them before installing, the script will produce an error.

>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'

>>>

Why do some modules need to be installed?

Every year, thousands of new modules are created by the global Python programming community.

It would be impractical to include them all by default in the interpreter. Additionally, not all of them are necessarily useful.

Note. Over time, only the most commonly used modules are integrated directly into the latest versions of the Python interpreter. Others must be installed separately.

How to install new modules in Python

To add new modules to Python, you can install them using Python's package manager PIP or set up an environment like Anaconda.

  • How to install PIP3
  • How to install ANACONDA on a Linux PC

Here are some tutorials for installing additional Python packages:

  • How to install MATPLOTLIB
  • How to install NUMPY
  • How to install PANDAS
  • How to install the NLTK (Natural Language Toolkit) library
  • How to install SKLEARN (Scikit Learn)
  • How to install SPACY (Natural Language Processing)

After installing them, you can import the modules into Python scripts or the console.

How to view the list of functions in a module

To discover what functions a module contains, type the module name followed by a dot.

After a few seconds, a dropdown menu will appear with all the module's functions.

Example

list of functions in the math module

Note. The module must already be imported into memory.

This makes it easier to find functions without having to remember them all.

 

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Python Modules