AppDividend
Latest Code Tutorials

Python Module vs Package: Complete Difference in 2021

0

Python programming has a tendency to put functions in one file and use them in other files, and that file is called the module. In the case of the package, when you import the package, only variables or functions, or classes in the __init__.py file of that package are directly visible, not sub-packages or modules.

Python Module

Modules in Python enable you to organize your code more efficiently. A module can define functions, classes, and variables. A module does not have to be very big, and it can be a simple file or group of files whose ultimate goal to create a reusable code and provide specific functionality. 

Grouping your code into a module makes more sense when analyzing the code, making it easier to understand and use.

A module is a single file that is imported into any file and uses it.

To use the Python module, we have to import the module in other files or the files you have to execute.

import your_module

To import modules from a different folder, you need to add the Python path at runtime. To add the Python path, use the sys.path.append() method, which includes locations such as the package.

Python Package

A package in Python is a collection of modules in directories that give a package hierarchy. A directory must contain a file named __init__.py to consider it as a package in Python. This file can be left empty, but we usually place the initialization code for that package in this file.

As our application program grows larger in size with many modules, we place similar modules in one package and different modules in different packages.

Importing module from a package

To import modules from packages in Python, use the dot (.) operator. For example, if we want to import the convert module from Excel, you can do the following:

 

Python Module vs Package

If we want to import the Excel’s convert.py module in the above example, it can be done as follows:

import Export.Excel.convert

You can see that every module contains the same file name but each module exports a different format. If the convert.py module contains a function named export_excel(), we must use the full name to reference it.

import Export.Excel.convert.export_excel()

Python Module vs Package

The main difference between module and package in Python is at the file system level. Any Python file is a module file. The organized module files create a package. When you import a module or a package, the corresponding object created by Python is always of type module.

In Python, all the packages are modules, but all modules are not packages because the module can be a single file. Any module that contains a __path__ attribute is considered a package.

A package is a module that may have submodules (including subpackages). Not all modules can do this. A package is represented by an imported top-entity which could either be a self-contained module or the __init__.py special module as the top-entity from a set of modules within a subdirectory structure.

That is it for the Python Module vs Package tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.