You are on page 1of 10

Packages In Python

• We don’t usually store all of our files in our computer in the same
location.similar files are kept in the same directory,for example,we may
keep all the songs in the “music” directory .same as this, python has
packages for directories and modules for files.
• As our application program grows larger in size with a lot of modules,we
place similar modules in one package and different modules in different
packages.This makes a program easy to manage and conceptually clear.A
python package can have sub-packages and modules.
• A directory must contain a file named __init__.py in order for python to
consider it as a package.This file can be left empty but we generally place
the initialization code for that package in this file.
EXAMPLE:Suppose we are developing a game,one
possible organization of packages and modules
could be shown below :
• Package Initialization
• If a file named __init__.py is present in a package directory, it is
invoked when the package or a module in the package is
imported. This can be used for execution of package
initialization code, such as initialization of package-level data.
• This __init__.py file can be kept empty or can place any initialization
code
Importing Module From a Package
• There are three ways to import modules from a package.They are:
1. We can import modules from packages using the dot operator.
Example: If we want to import the start module from the above
example fig,it can be done as follows :

Import Game.Level.start
-> Now if this module contains a function named select_difficulty(),we
must use the full name to reference it. As ,
Game.level.start.select_diffuculty(2)
2. The second way to import is , importing the module without the
package prefix as,
from Game.Level import start
-> Now,we can call the function simply as follows ,
Start.select_difficulty(2)
3. The another way is importing just the required function (or class or
variable) from a module within a package would be as,
from Game.Level.start import select_difficulty
-> Now we can directly call this function
Select_difficulty(2)
Creating Our Own Package
1. Go to PC -> Documents -> new-> folder -> Give name of the package
as mypackage1
2. In mypackage1-> new-> file->__init__.py
3.In mypackage1-> new-> file-> demo.py
4. Write some code in demo.py file as,( by opening with python IDE )
def demoprint():
Print (“ now in the demo file!” )
5. Now create another package let it be mypackage2 and write the below code to access
demoprint () method from mypackage1.
. Import mypackage1.demo
mypackage1.demo.demoprint()
Output: now in the demo file!
Example of creating our own Package
• Let's create a package named mypackage, using the following
steps:
• Create a new folder named D:\MyApp.
• Inside MyApp, create a subfolder with the name 'mypackage'.
• Create an empty __init__.py file in the mypackage folder.
• Using a Python-aware editor like IDLE, create modules greet.py
and functions.py with following code:
greet.py
def SayHello(name):
print("Hello " + name)
return
functions.py
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
• That's it. We have created our package called
mypackage.
Importing a Module from a Package
Now, to test our package, invoke the Python
prompt from the MyApp folder.
D:\MyApp>pythonImport the functions module from
the mypackage package and call its power()
function.
>>> from mypackage import functions
>>> functions.power(3,2)
9
Subject : Python
Topic : Packages

• Name : ZEENATH KOUSAR
• Std :. MCA 1st year (semester 1)
• Roll no : 01

You might also like