0% found this document useful (0 votes)
6 views

Python - Package

Uploaded by

madhus.naragani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python - Package

Uploaded by

madhus.naragani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Science – Python Package

14. Python Package

Contents
1. What is a package? ............................................................................................................................. 2
2. __init__.py file .................................................................................................................................... 2
3. Advantage........................................................................................................................................... 2

1|Page 14.Python Package


Data Science – Python Package

14. Python Package

1. What is a package?

✓ A package is nothing but folder or directory which represents collection


of python modules(programs)

2. __init__.py file

✓ Any folder or directory contains __init__.py file, is considered as a


Python package.
✓ __init__.py can be empty file.
✓ A package can contain sub packages also.

3. Advantage

✓ We can resolve naming conflicts.


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

2|Page 14.Python Package


Data Science – Python Package

Example1
|
|---demo1.py
|
|---demo2.py
|
|----- __init__.py
|
|---pack1
|
|----- test1.py
|
|----- __init__.py

Program Creating __init__.py file


Name __init__.py

Empty file

Program executing a package


Name test1.py

def m1():
print("Hello this is test1 present in pack1")

3|Page 14.Python Package


Data Science – Python Package

Program executing a package


Name demo1.py

import pack1.test1
pack1.test1.m1()

output
Hello this is test1 present in pack1

Program executing a package


Name demo2.py

from pack1.test1 import m1


m1()

output

Hello this is test1 present in pack1

4|Page 14.Python Package


Data Science – Python Package

Example2
|
|---demo3.py
|
|------__init__.py
|
|---maindir
|
|------test2.py
|
|------__init__.py
|
|------ subdir
|
|----test3.py
|
|----__init__.py

Program package
Name __init__.py

Empty file

Program executing a package


Name test2.py

def m2():
print("This is test2 present in maindir")

5|Page 14.Python Package


Data Science – Python Package

Program executing a package


Name test3.py

def m3():
print("This is test3 present in maindir.subdir")

Program executing a package


Name demo3.py

from maindir.test2 import m2


from maindir.subdir.test3 import m3

m2()
m3()

output
This is test2 present in maindir
This is test3 present in maindir.subdir

Make a note

✓ Summary diagram of library, packages, modules which contains


functions, classes and variables.
o Library - A group of packages
o Package - A group of modules
o Modules - A group of variables functions and classes.

6|Page 14.Python Package

You might also like