DEV Community

Srinivasulu Paranduru
Srinivasulu Paranduru

Posted on

Python Packages & Sub Packages

  • Python packages are a way to organize and structure code by grouping related modules into directories.

  • A package is essentially a folder that contains an init.py file and one or more Python files (modules).

  • Allows modules to be easily shared and distributed across different applications.

Key Components of a Python Package
Module: A single Python file containing reusable code (e.g., math.py).
Package: A directory containing modules and a special init.py file.
Sub-Packages: Packages nested within other packages for deeper organization.

What is init.py

The init.py file tells Python that a directory should be treated as a package, and it can contain code to initialize the package, control its public API, or perform setup tasks when it is imported.

Demo:
Please download the code form the github repo

Structure of the repo looks like

** I have download github repo code to C:\PYTHONTRAINING**

C:\PYTHONTRAINING
│ myprogram.py

└───MainPackage
│ some_main_script.py
init.py

└───SubPackage
mysubscript.py
subpackage2.py
init.py

** Run the python file myprogram.py and output as follows**

Top comments (0)