0% found this document useful (0 votes)
3 views4 pages

Chap 11 Packagesword

The document explains Python packages as a way to group related modules, with a package being a directory containing an __init__.py file. It highlights the advantages of using packages, such as resolving naming conflicts and improving modularity. Examples illustrate how to create and use packages and modules in Python.
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)
3 views4 pages

Chap 11 Packagesword

The document explains Python packages as a way to group related modules, with a package being a directory containing an __init__.py file. It highlights the advantages of using packages, such as resolving naming conflicts and improving modularity. Examples illustrate how to create and use packages and modules in Python.
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/ 4

Packages

It is an encapsulation mechanism to group related modules into a single unit.


package is nothing but folder or directory which represents collection of Python modules.

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

A package can contains sub packages also.

init .py

File 1

File 1 File 1

init .py init .py

x.py y.py m.py n.py

Home Loan Vehicle Loan

Loan

nd
SKILLSOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www. skillsoft.com
init .py

File 1

File 1 File 1

init .py init .py

Module 1 Module 1 Module 1 Module 1

Home Loan Vehicle Loan

Loan

The main advantages of package statement are

1. We can resolve naming conflicts


2. We can identify our components uniquely
3. It improves modularity of the application

Eg 1:

D:\Python_classes>
|-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()

nd
SKILLSOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www. skillsoft.com
test.py (version-2):

from pack1.module1 import f1


f1()

Eg 2:

D:\Python_classes>
|-test.py
|-com
|-module1.py
|- init .py
|- skillsoft
|-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. skillsoft")

test.py:

1. from com.module1 import f1


2. from com. skillsoft.module2 import f2
3. f1()
4. f2()
5.
6. Output
7. D:\Python_classes>py test.py
8. Hello this is from module1 present in com
9. Hello this is from module2 present in com. skillsoft

Note: Summary diagram of library,packages,modules which contains functions,classes


and variables.

nd
SKILLSOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www. skillsoft.com
Library

pack 1 pack 2 --------------- pack n

module 1 module 2 module n module 1 module 2 module n

function 1 function 2 function n variables classes

nd
SKILLSOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www. skillsoft.com

You might also like