0% found this document useful (0 votes)
14 views6 pages

Understanding Python Packages in Data Science

Uploaded by

madhus.naragani
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)
14 views6 pages

Understanding Python Packages in Data Science

Uploaded by

madhus.naragani
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

Data Science – Python Package

14. Python Package

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

1|Page [Link] 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 [Link] Package


Data Science – Python Package

Example1
|
|---[Link]
|
|---[Link]
|
|----- __init__.py
|
|---pack1
|
|----- [Link]
|
|----- __init__.py

Program Creating __init__.py file


Name __init__.py

Empty file

Program executing a package


Name [Link]

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

3|Page [Link] Package


Data Science – Python Package

Program executing a package


Name [Link]

import pack1.test1
pack1.test1.m1()

output
Hello this is test1 present in pack1

Program executing a package


Name [Link]

from pack1.test1 import m1


m1()

output

Hello this is test1 present in pack1

4|Page [Link] Package


Data Science – Python Package

Example2
|
|---[Link]
|
|------__init__.py
|
|---maindir
|
|------[Link]
|
|------__init__.py
|
|------ subdir
|
|----[Link]
|
|----__init__.py

Program package
Name __init__.py

Empty file

Program executing a package


Name [Link]

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

5|Page [Link] Package


Data Science – Python Package

Program executing a package


Name [Link]

def m3():
print("This is test3 present in [Link]")

Program executing a package


Name [Link]

from maindir.test2 import m2


from [Link].test3 import m3

m2()
m3()

output
This is test2 present in maindir
This is test3 present in [Link]

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 [Link] Package

You might also like