Python Programming
About Lesson

A package in Python is like a folder in Windows which is usually stores the files and other folders. Similar to that, a python package may contain another packages (sub packages) or modules in it. In order to use a package in our working program the package must be residing in the path that is known to our Python IDE. We already know all the known paths are stored in sys.path variable. We have to add the new unknown path of your package to the sys.path variable.

import sys
sys.path.append(“path of package”)

#This program needs to create two packages and respective modules
#create a package newPkg in current working folder
#create module ModA under newPkg and save the following code
def disp():
print(“Hi from ModA”)

#create module ModB under newPkg and save the following code
def show():
print(“Hi from ModB”)

#create a package PythonProject on your desktop
#create module mod1and save the following code
x=10
def func1():
print(“func1 code”)

def func2():
print(“func2 code”)

#create new python file in your current working folder and save the following code
import sys
sys.path.append(“C:\\Users\\SatyamITGURU\\Desktop”) #adding the unknown path

from newPkg.ModA import * #newPkg is created inside current working folder
from PythonProject.mod1 import *

disp() #disp() is the module defined in ModA
func2() #func2() is the module defined in mod1

You cannot copy content of this page