Ticker

6/recent/ticker-posts

Python: Packages

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

the package is nothing but a folder or directory which represents a 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.

package

package

The main advantages of a 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()


test.py (version-2):

from pack1.module1 import f1

f1()

Eg 2:

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

test.py:

1. from com.module1 import f1 
2. from com.durgasoft.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.durgasoft 
Python: Packages

"Python Packages"

"Python Packages List"

"Python Packages For Data Science"

"Python Packages Example"

"Python Packages For Machine Learning"

"How To Install Python Packages"

"Check Installed Python Packages"

"Python Update All Packages"

"Python Extension Packages"