What are modules?

Modules in Python are separate code groupings which packages program code and data for reuse. Since modules are separate files, you need to tell Pthon where to find the file to read it into your application. This is usually done using the import or from statements.

Some of the advantages of using modules in your Python code are:

  • they enable you to organize your code into smaller pieces that are easier to manage.
  • the code in the modules can be reloaded and rerun as many times as needed, which allows for code reuse.
  • they are self-contained – you can never see a name in another file, unless you explicitly import it. This helps you to avoid name clashes across your programs.
Commonly used modules that contain the source code for generic purposes are known as libraries.

 

Modules are usually stored in files with .py extension. The collection of classes, functions, and variables inside a module is known as attributes. These attributes can be accessed using their names.

Geek University 2022