0% found this document useful (0 votes)
12 views

Module

Uploaded by

swarna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module

Uploaded by

swarna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Python Modules

Modules allows to reuse one or more function in


the program, even in the programs in which
those functions have not been defined.

A module is .py extension file which has the


Module definition of all the functions and variables that
we can use in other programs.

The program which wants to use functions and


variables defined in the module will use import
keyword to link or import the module or the .py
file.
•Python First Searches in the current
working directory.
•Second search is in the directory
Module specified in the PYTHONPATH
Loading and environment variable.

Execution •Lastly searches the Python installation


path.
If not found anywhere then Import
Error exception is generated.
How to To create a module use .py
Create a extension to save your
Module? code in a file.
The import statement is used to
How to use import all the functionality of the
a Module? module into another python program.

Example
The from math import sqrt
from...import
Statement print(sqrt(8))
A module can also contain
Variables in variables like arrays,
Module dictionaries, objects etc.
Example.
"as" keyword can be
Renaming a used to rename a
Module Module.
Example
Using dir() dir() is a built-in function which lists the
identifiers defined inside a module.
function These identifiers may be functions, classes
and variables.
#module1
def new(x):
return x*3
#module2
Scope of def new(x):
Variables return x**3
import module1
import module2
ans=new(10)
Each module in Python belongs to a namespace.
This namespace includes name of all the function
and variables defined inside a module.

Scope of When a program executes, three namespaces are


referred:
Variable I. built-in namespace
II. Local namespace
III. Global namespace
Built-in namespace contains name of all the
built-in functions and constant.
Local namespace contains variables
declared inside the function.
Scope of Global namespace contains all the identifiers
Variable declared inside a module.
Python interpreter when it encounters an
identifier it first searches the local
namespace and after that the global name
space and finally built-in name space.
By default, all the identifiers defined inside a
module are public i.e. they are accessible by
Private any module importing them.

Module In python two underscore __ can be placed


before an identifier to make it a private
identifier.
A Package is a hierarchical file directory
structure.
It consists of modules and other packages
within it.
Python Every package in Python is a directory which
Packages must have a special file called _init_.py
_init_.py file indicates that the directory
contains a Python Package.
Let's create a package named pack in the
home directory. Consider the following steps.
1. Create a directory with name pack on path
/home.
2. Create a python source file with name a.py
Python and b.py on the path /home/Students.

Package 3. To make this directory a package, we need


to include one more file here, that is
__init__.py which contains the import
statements of the modules defined in this
directory.
Now, the directory Pack has become the
Python package containing a python modules. Here
we must notice that we must have to create
Package __init__.py inside a directory to convert this
directory to a package.

You might also like