Python Package
Creating and Exploring Packages, Example of Creating Package, Various ways of Accessing the Packages
Gaurav Kr. suman 4/19/20 DSPMU_MCA/MIT
A package is a hierarchical file directory structure that defines a single Python application
environment that consists of modules and subpackages and sub-subpackages, and so
on.
We don't usually store all of our files in our computer in the same location. We use a
well-organized hierarchy of directories for easier access.
Similar files are kept in the same directory, for example, we may keep all the Images in
the "Image" directory. Analogous to 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
project (program) easy to manage and conceptually clear.
Similar, as a directory can contain sub-directories and files, 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.
Here is an example. Suppose we are developing a CAR, one possible organization of
packages and modules could be as shown in the figure below.
1|Page
To tell Python that a particular directory is a package, we create a file named __init__.py
inside it and then it is considered as a package and we may create other modules and
sub-packages within it. This __init__.py file can be left blank or can be coded with the
initialization code for the package.
To create a package in Python, we need to follow these three simple steps:
1. First, we create a directory and give it a package name, preferably
related to its operation.
2. Then we put the classes and the required functions in it.
3. Finally we create an __init__.py file inside the directory, to let Python
know that the directory is a package.
Let’s look at this example and see how a package is created. Let’s create a package
named Cars and build three modules in it namely, Bmw, Audi and Nissan.
1. First we create a directory and name it Cars.
2. Then we need to create modules. To do this we need to create a file with the
name Bmw.py and create its content by putting this code into it.
class Bmw:
def __init__(self):
self.models = ['i8', 'x1', 'x5', 'x6']
def outModels(self):
print('These are the available models for BMW')
for model in self.models:
print('\t%s ' % model)
Then we create another file with the name Audi.py and add the similar type of code to it
with different members.
class Audi:
def __init__(self):
self.models = ['q7', 'a6', 'a8', 'a3']
def outModels(self):
print('These are the available models for Audi')
for model in self.models:
print('\t%s ' % model)
2|Page
Then we create another file with the name Nissan.py and add the similar type of code to
it with different members.
class Nissan:
def __init__(self):
self.models = ['altima', '370z', 'cube', 'rogue']
def outModels(self):
print('These are the available models for Nissan')
for model in self.models:
print('\t%s ' % model)
3. Finally we create the __init__.py file. This file will be placed inside Cars directory
and can be left blank or we can put this initialisation code into it.
from Bmw import Bmw
from Audi import Audi
from Nissan import Nissan
Now, let’s use the package that we created. To do this make a sample.py file in the
same directory where Cars package is located and add the following code to it:
from Cars import Bmw
from Cars import Audi
from Cars import Nissan
# Create an object of Bmw class & call its method
ModBMW = Bmw()
ModBMW.outModels()
# Create an object of Audi class & call its method
ModAudi = Audi()
ModAudi.outModels()
# Create an object of Nissan class & call its method
ModNissan = Nissan()
ModNissan.outModels()
3|Page
1. import in Packages
Suppose the cars and the brand directories are packages. For them to be a package
they all must contain __init__.py file in them, either blank or with some
initialization code. Let’s assume that all the models of the cars to be modules. Use
of packages helps importing any modules, individually or whole.
Suppose we want to get Bmw i8. The syntax for that would be:
'import' Cars.Bmw.i8
While importing a package or sub packages or modules, Python searches the whole
tree of directories looking for the particular package and proceeds systematically as
programmed by the dot operator.
If any module contains a function and we want to import that. For e.g., a8 has a
function get_buy(1) and we want to import that, the syntax would be:
import Cars.Audi.a8
Cars.Audi.a8.get_buy(1)
While using just the import syntax, one must keep in mind that the last attribute
must be a subpackage or a module, it should not be any function or class name.
2. ‘from…import’ in Packages
Now, whenever we require using such function we would need to write the whole
long line after importing the parent package. To get through this in a simpler way
we use ‘from’ keyword. For this we first need to bring in the module using ‘from’
and ‘import’:
from Cars.Audi import a8
Now we can call the function anywhere using
a8.get_buy(1)
There’s also another way which is less lengthy. We can directly import the function
and use it wherever necessary. First import it using:
from Cars.Audi.a8 import get_buy
Now call the function from anywhere:
get_buy(1)
4|Page
3. ‘from…import *’ in Packages
While using the from…import syntax, we can import anything from submodules to
class or function or variable, defined in the same module. If the mentioned
attribute in the import part is not defined in the package then the compiler throws
an ImportError exception.
Importing sub-modules might cause unwanted side-effects that happens while
importing sub-modules explicitly. Thus we can import various modules at a single
time using * syntax. The syntax is:
from Cars.Chevrolet import *
5|Page