/  Technology   /  Difference between python module and python package?

Difference between python module and python package?

What’s the difference between a Python module and a Python package?

Module: It is a simple Python file that contains collections of functions and global variables and has a “.py” extension file. It’s an executable file and we have something called a “Package” in Python to organize all these modules.

Package: It is a simple directory which has collections of modules, i.e., a package is a directory of Python modules containing an additional __init__.py file. It is the __init__.py which maintains the distinction between a package and a directory that contains a bunch of Python scripts. A Package simply is a namespace. A package can also contain sub-packages.

When we import a module or a package, Python creates a corresponding object which is always of type module. This means that the dissimilarity is just at the file system level between module and package.

Note: When we try to import a package, the sub-packages or modules aren’t often visible i.e., only the variables or functions or classes in the package’s __init__.py file are directly visible.

For example, in the datetime module, there’s a submodule named date which doesn’t get imported, if we just import the datetime module. We’ll have to import it separately.

 

Leave a comment