0% found this document useful (0 votes)
5 views2 pages

9.Packages in Python

The document explains the concept of Python packages, which are directories containing related modules and an __init__.py file. It highlights the advantages of using packages, such as resolving naming conflicts and improving modularity. Two examples demonstrate how to create and use packages in Python, showcasing the importation of functions from different modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

9.Packages in Python

The document explains the concept of Python packages, which are directories containing related modules and an __init__.py file. It highlights the advantages of using packages, such as resolving naming conflicts and improving modularity. Two examples demonstrate how to create and use packages in Python, showcasing the importation of functions from different modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Leela Soft Python3 Madhu

Packages
✓ It is an encapsulation mechanism to group related modules into a single unit.
✓ The package is nothing but folder or directory which represents collection of Python
modules.
✓ A package can contains sub packages also.

Any folder or directory contains __init__.py file, is considered as a Python package. This
file can be empty.

The main advantages of package statement are


✓ We can resolve naming conflicts
✓ We can identify our components uniquely
✓ It improves modularity of the application

Example 1:
E:\8am>
|-test.py
|-pack1
|-module1.py
|-__init__.py

__init__.py: empty file

module1.py:
def f1():
print("Hello this is from module1 present in pack1")

test.py (version-1):
import pack1.module1
pack1.module1.f1()

test.py (version-2):
from pack1.module1 import f1
f1()

www.leelasoft.com Cell: 78-42-66-47-66 1


Leela Soft Python3 Madhu

Example 2:
E:\8am>
|--test.py
|--com
|--module1.py
|--__init__.py
|--leelasoft
|--module2.py
|--__init__.py

__init__.py: empty file

module1.py:
def f1():
print("Hello this is from module1 present in com")

module2.py:
def f2():
print("Hello this is from module2 present in com.leelasoft")

test.py:
from com.module1 import f1
from com.leelasoft.module2 import f2
f1()
f2()

www.leelasoft.com Cell: 78-42-66-47-66 2

You might also like