Last Updated : 15 Feb, 2018
Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access. Just like there are different drives and folders in an OS to help us store files, similarly packages help us in storing other sub-packages and modules, so that it can be used by the user when necessary.
Creating and Exploring Packages
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:Example of Creating 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.
# Python code to illustrate the Modules
class Bmw:
# First we create a constructor for this class
# and add members to it, here models
def __init__(self):
self.models = ['i8', 'x1', 'x5', 'x6']
# A normal print function
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. Python
# Python code to illustrate the Module
class Audi:
# First we create a constructor for this class
# and add members to it, here models
def __init__(self):
self.models = ['q7', 'a6', 'a8', 'a3']
# A normal print function
def outModels(self):
print('These are the available models for Audi')
for model in self.models:
print('\t%s ' % model)
Then we create another file with the name Nissan.py and add the similar type of code to it with different members. Python
# Python code to illustrate the Module
class Nissan:
# First we create a constructor for this class
# and add members to it, here models
def __init__(self):
self.models = ['altima', '370z', 'cube', 'rogue']
# A normal print function
def outModels(self):
print('These are the available models for Nissan')
for model in self.models:
print('\t%s ' % model)
from Bmw import Bmw
from Audi import Audi
from Nissan import Nissan
# Import classes from your brand new package
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()
Various ways of Accessing the Packages
Let's look at this example and try to relate packages with it and how can we access it.'import' Cars.Bmw.x5While 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.
from Cars.Audi import a8Now 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_buyNow call the function from anywhere:
get_buy(1)
from Cars.Chevrolet import *This will import everything i.e., modules, sub-modules, function, classes, from the sub-package.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4