Python Modules and Packages | Infogen Labs
Ciklum India
We develop Digital Solutions for Fortune 500 and fast-growing organisations alike around the world.
Python modules are those parts or files which have functions, and statements written into it. There can be multiple modules with different sets of functions, classes or variables and different execution methodology. Modifying these files have to be done separately to avoid interference of other modules.
For Ex- While designing e-commerce websites, one module can be responsible for displaying products and other regarding adding them into cart.
?
●?????.py extension is used for these files or modules in python.
●?????These files can contain functions, variables, classes etc.
●?????These files are reusable anywhere in programme.
●?????These can be used at any time during the programme.
?
Creating module
This can be done easily by writing a few code lines in file and saving it with extension .py.
For Ex:
???????????def round1(player_name):
???????????????print(player_name)
?
Note: This module has been saved with game.py
Import statement - These modules can be imported in programme by using the import keyword.
Syntax
????????Import modulename
Ex:
???????Import game
??????game.round1(“robert”)
Output: robert
Importing all objects or parts of a module - we can import all objects or parts or items defined in the module using the import keyword “from”.
Syntax:
1. from modulename import * -it will import all names present in the module.
2. from modulename import function - It will import particular function present in module
3.from modulename?import variable?- it will import variables?present in the module.
How does the interpreter work with the import statements?
In a programme where an import statement is used, the Interpreter searches module name in the list of?directories such as current directory,directories in PYTHONPATH environment variable and this process is?known as module search path.
?
Note -Location of module can also be accessed using __file__ attribute.
Import game
game.__file__
Output:
/desktop/folder1/ModulesAndpackages/game.py
Built-in modules - These modules are written in c but integrated with python.We can check it by using statement?help(‘modules’).
Packages - Packages use a modular approach. Packages?act as a directory or folder containing different modules and subpackages. Packages must have __init__.py file.This file makes interpreters treat the folder as a package even if it is?empty. "Package not found" error will occur if i__init__.py file is not present in the package.
For Ex:?
We create folder school then we will be creating modules to store in it.
1.class.py
def class_number():
???for i in range(10):
?print(i)
class_number()
?
2.student.py
def student_name(1,41):
???i=input(“Enter student name:?“)
?print(i)
student_name()
Importing package and relative functions.
We can import packages in?file by writing the following code
From school import class_number,student_name
class_number()
student_name()
Note: PyPi(python package index) is a package repository containing various already available developed packages.
Difference between packages and modules:
●?????Module is a single file containing objects while the package is containing multiple files.
●?????Importing in module is done by using * but this is not the same in the package.
●?????Packages contain __init__.py file while modules are not containing it.
?
" Author:- Divyanshi S. "
Visit Us:?corp.infogen-labs.com?