What Is Module Package

Q

What Is Module Package in Python?

✍: FYIcenter.com

A

A module package in Python is a collection of Python module files saved in a file directory with a special file called __init__.py.

A module package can have the following elements:

  • Package name - A symbolic name that uniquely identifies this module package within the context. By default, the package name is the file directory name, where the Python module files are located in.
  • Package properties - All variables defined in the __init__.py in the package directory are package properties. They can accessed using the dot (.) expression format of package_name.property_name.
  • Package methods - All functions defined in the __init__.py in the package directory are package methods. They can accessed using the dot (.) expression format of package_name.method_name.
  • Package classes - All classes defined in the __init__.py in the package directory are package classes. They can accessed using the dot (.) expression format of package_name.class_name.
  • Package modules - All modules defined in module files in the package directory are package modules. They can accessed using the dot (.) expression format of package_name.module_name.

Module packages can be nested in the same way as file directories can be nested.

For example, we can created a module package called "firstPackage" by creating the following file directory and the __init__.py file:

>\fyicenter> dir
 <DIR>          firstPackage

>\fyicenter> dir firstPackage
        337 secondModule.py
        165 __init__.py

>\fyicetner> type firstPackage\__init__.py
# __init__.py
"""My first module package"""
1.00 = "2.0"
print("Loading first package...")

def sayHello():
    print("Greetings from first package!")

 

'import' Module Package Loading Statement

Modules Are Objects Too

Defining and Using Python Code Modules

⇑⇑ Python Tutorials

2022-08-26, 1192🔥, 0💬