Modules in Python
Introduction to Modules and Packages in
Python.
• A library is a collection of related functions.
• In Python, library is a set of packages and every package may have
one or more modules.
• Each Module can have many functions.
• The reason for this hierarchical design is to extend the functionality of
the python language.
• A module in like a library, like a library, a module greatly extends the
functionalities of Python
Creation of a Module
• Modules in python are the collection of functions as single or multiple
files.
• The Modules are created as a usual python file with the “.py”
extension.
• Example: Illustrating the creation of module to find the factorial and
permutation.
Importing Modules
• The Process of importing can be performed for the following
activities.
1. Using importing operation, everything inside the module can be
imported.
2. Certain named variables and certain functions can be imported
from a module.
The Syntax for importing the entire module is shown below.
• The import statement performs the following two activities.
1. Find the given module in the directory and follow by loading the
module to the environment and initializing the module. The process
of initialization is not mandatory for all packages and modules.
2. Registering the imported successfully, it will be made available in
the local namespace in one of the three ways.
If no other name is specifies, and the module being imported is a top-
level module, the module’s name is bound in the local namespace as a
reference to the imported module.
Importing Submodule of a Module
• The scope of a module is larger than a simple function and a single
class. For practical purposes, a module could consist of more than
one function. In such cases, Python allows us to import specific
submodules using the following syntax.
Types of Imports
• There are two types of imports in Python, namely, absolute import and
relative import.
• Absolute Import involves importing the entire module from its root folder
and also demands the programmer to utilize the properties of the
modules.
• Relative import specifies an object or module imported from its current
working directory, that is the location where the import statement resides.
• There are two types of relative imports.
1. Implicit relative imports
2. Explicit relative imports
Uses of Standard Modules in Python
• When a Python interpreter shell is opened, some built-in functions
are loaded by default.
• One could notice that, the functions, such as print() and input(), and
number conversion functions, such as int(), float(), and complex() are
available by default and no need for any import operations.
• Among the available standard modules in Python, a few modules are
considered important modules for a python programmer
Math and Sympy Packages
os Module
• The os module provides a handful way of utilizing the operating
system based functionalities, such as file descriptors handling,
process handling, path handling, and so on.
General os functions
os.name(): os.name() returns the name of the currently working
operating system.
Process parameters:
os.environ: os.environ is a class object that consists of all the locations
and is stored in the system environment variables that hold the variable
name and also its values.
sys Module
• The sys module provides system specific parameters and functions
that allow the programmer too access some variables that are
maintained by the interpreter and functions that interact with the
interpreter strongly.
• sys.argv: sys.argv is a variable of list data type that stores the
command line arguments that are passed to a module of python
while executing it.
• The general syntax for giving a command line argument is as follows.
Packages
• A package is a folder that has one or more modules and is
differentiated from other folder by the special method called
“__init__.py”
Differences between Packages and Modules
Packages Modules
• Package holds __init__.py for every • Modules need not have __init__.py
user oriented code. for any user specific codes.
• Package is a directory and various • The module is a simple Python file
subdirectory that contains a that contains the collection of
collection of modules and functions and global variables.
__init__.py in the directory and • Simple.py script with functions,
every sub-directories. classes, and objects constitutes a
• A directory contains Python module.
modules and also has a __init__.py
file by which the interpreter
interprets it as a package