Nearby lessons

106 of 108

Python - Virtual Environment

Detailed Tutorial Notes

The main advantages of package statement are

  • We can resolve naming conflicts
  • We can identify our components uniquely
  • 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()

Loan

File 1

__init__.py

File 1

Module 1

Home Loan

Module 1

__init__.py

Vehicle Loan

File 1

Module 1 Module 1

__init__.py

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:

  • from com.module1 import f1
  • from com.durgasoft.module2 import f2
  • f1()
  • f2() 5.
  • Output
  • D:\Python_classes>py test.py
  • Hello this is from module1 present in com
  • Hello this is from module2 present in com.durgasoft Note: Summary diagram of library,packages,modules which contains functions,classes and variables.

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Python - Command Line Arguments