Skip to content

๐Ÿ“ฆ Python Packages, Modules, and Libraries

Here you can find the difference between the ๐Ÿ“ฆ packages, ๐Ÿ“š modules, and ๐Ÿ“– libraries in Python.

In Python, a ๐Ÿ“š module is a file that contains Python code. It can include variables, functions, classes, and other programming elements. Modules are used to organize and reuse code in Python. You can create your own modules to divide your code into logical parts and then import them into other scripts or modules to utilize their functionalities. For example, if you have a file named "mymodule.py" that contains a function called "greet()", you can import that module into another Python file and call the "greet()" function.

A ๐Ÿ“ฆ package is a way to organize related modules in a directory. A package is simply a folder that contains one or more Python modules, along with a special file called "init.py" that indicates the folder is a package. Packages are used to structure and organize larger and more complex Python projects. You can think of packages as folders that contain modules and other packages. Packages can also include ๐Ÿ“ฆ subpackages, allowing for a hierarchical structure to organize the code.

Lastly, a ๐Ÿ“– library in Python is a collection of modules and/or packages that are grouped together to provide additional functionalities. A library can contain modules and/or packages that offer a wide range of specific functionalities and utilities. In Python, there are many popular and widely used libraries, such as NumPy, Pandas, Matplotlib, TensorFlow, among others. These libraries provide sets of specialized functions and tools for tasks like numerical calculations, data manipulation, graph visualization, machine learning, etc. You can install additional libraries in your Python environment using tools like pip (the Python package manager) to expand the capabilities of Python and leverage additional functionalities without having to implement everything from scratch.

This diagram illustrates how modules are organized into packages and how libraries can contain multiple modules.

โ””โ”€โ”€ ๐Ÿ“‚ library/
    โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
    โ”œโ”€โ”€ ๐Ÿ“„ module1.py
    โ”œโ”€โ”€ ๐Ÿ“„ module2.py
    โ””โ”€โ”€ ๐Ÿ“„ module3.py
    โ”œโ”€โ”€ ๐Ÿ“ฆ package/
    โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
    โ”‚ โ””โ”€โ”€ ๐Ÿ“„ another_module.py
    โ”‚ โ”œโ”€โ”€ ๐Ÿ“ฆ subpackage/
    โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
    โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ another_module.py
  • ๐Ÿ“‚ library/: A folder representing a library that contains multiple modules.
  • ๐Ÿ“„ init.py: A special file indicating that the library root.
  • ๐Ÿ“„ module1.py, ๐Ÿ“„ module2.py, ๐Ÿ“„ module3.py: Modules within the library.
  • ๐Ÿ“ฆ package/: A folder representing a package that contains other modules and/or subpackages.
    • ๐Ÿ“„ init.py: A special file indicating that the folder package/ is a package.
    • ๐Ÿ“„ another_module.py: Another module within the package package/.
    • ๐Ÿ“ฆ subpackage/: A subpackage within the package/ that also contains an __init__.py file.
    • ๐Ÿ“„ init.py: A special file indicating that the folder subpackage/ is a package.
    • ๐Ÿ“„ another_module.py: Another module within the subpackage subpackage/.

In summary, modules are individual files that contain Python code, packages are folders that contain modules and/or subpackages organized hierarchically, and libraries are collections of modules and/or packages that provide specialized functionalities for specific tasks.