Python Modules and Packages
Python module is defined as a collection of functions, variables, constants, classes, objects along with Python statements.
Structure of a Python Module
A Python module is a normal Python file (.py file) that can contain following things:
Docstring is text written within the pair of triple quotes. it is basically used for documentation purpose. Docstring should be the first string stored inside a module, function or class.
General conventions for Doctstring are:
- First letter of first line is a capital letter.
- Second line is blank line.
- Rest of the details begin from third line.
(ii) Variables and constants
We can also define variables and constants inside a Python module depending upon the requirement.
We can also create more than class inside a Python module. Class may be defined as blueprint for creating objects.
Object is anything having properties and behaviour. But in Python, object is just a variable of class type which canbe used to refer to members of a class. It is also known as instance of class.
We can write all valid statements of Python within a module.
Function is defined as named group of instructions. When we call function, all statements written inside the function body get executed.
Types of Python Module
There are two types of modules in Python
- Predefined modules
- User Defined Modules
(i) Predefined Modules
Predefined modules are also known as library modules. They contain library functions and classes provided by Python. These modules are used to perform basic complex mathematical operations and many other operations which can’t be performed by any user defined modules. We can import predefined modules in any Python program.
(ii) User Defined Modules
User Defined Modules are defined by a programmer depending upon his own requirement. Once a user defined module is defined, it can be called within another Python module or program.
''' Name: Demonstration of Python modules What it contains: It contains two functions namely sum() and subtract() ''' def sum(a,b): return(a+b) def subtract(a,b): return(a-b)
In above example a user defined module named Module1.py has been created.
We can see documentation of above program by using following command at python shell.
1. import Module1 #to import module
2. help(Module1) #To see documentation of module
>>> import Module1 >>> help(Module1) Help on module Module1: NAME Module1 FILE c:\python30\Module1.py DESCRIPTION Name: Demonstration of Python modules What it contains: It contains two functions namely sum() and subtract() FUNCTIONS subtract(a, b) sum(a, b)
We can also use predefined function dir() to see list of elements defined inside a module as follows:
>>>import Module1 >>> dir(Module1) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'subtract', 'sum']
Importing Modules in a Python Program
Before using functions and other components of a module in a Python program, we need to import the module in our program.
We can use import statement to import modules in a program. The import statement can be used in two ways:
- To import entire module: the
- To import selected objects
(i) To import entire module
We can use import<module-name>command to import entire module. The import statement internally executes the code written inside module file and then makes it available to our program.
Syntax to import entire module is:
import module1[, module2[,…module3]]
module1,module2,module3 refer to names of different modules that we want to import in our program.
To import predefined module math in a Python program we can use import math statement in a Python program.
To import user defined module Module1 in a Python program we can use import Module1 statement in a Python program.
To import predefined modules math and random in a Python program we can use import math.randome statement in a Python program.
After importing a module, you can use any function or other element of the imported module as per following syntax:
To call sqrt() function defined in module math, we can write math.sqrt() statement in Python program.
To call sum() function defined in module Module1, we can write Module1.sum() in Python program.
(ii) To Imported Select Objects from a module
If you want to import some selected items, not all from a module, then you can use from <module> import statement as per following syntax:
To import a single object from a module we don’t have to prefix the module’s name, we can write the name of object after keyword import.
For example to import just the constant pi from module math, you can write:
from module math import pi
Now, we can use the constant pi. We need not prefix it with module name. We can simply write print(pi) statement at Python shell to get value of pi.
To import multiple objects from a module we don’t have to prefix the module’s name, we can write the comma separated list of objects after keyword import.
For examle, to import functions sqrt() and pow() from math module, we need to write from module math import sqrt, pow
To import all the items from a module we don’t have to prefix the module’s name, we can write:
To import all the items in module math,we can write:
from math import *
Now, we can use all the functions, variables etc from math module, without having to prefix module’s name to the imported item name.
|Program to import and use module Module1.py in program.|
import Module1 print(Module1.sum(10,5)) print(Module1.subtract(10,5))
|Program to import and use module math in program.|
import math print(math.sqrt(9)) print(math.pow(2,3))
|Program to import and use specific functions of module math.|
from math import sqrt,pow print(sqrt(9)) #No need to prefix math print(pow(2,3))
|Program to import all functions of module math.|
from math import * print(sqrt(9)) #No need to prefix math print(pow(2,3))
Processing of Import <module> Command
When we use import <module> command, internally following things take place:
- The code of imported module is interpreted and executed.
- Defined functions and variables created in the module are now available to the program that imported module.
- For imported module, a new namespace is setup with the same name as that of the module.
Namespace in Python is a named environment containing logical grouping of related objects. For every module(.py file), Python creates a namespace having its name similar to that of module’s name.
For example after importing module Module1 in our program ,all objects of module Module1 would be referred as Module1.<object-name>.
Example: if Module1 has a function defined as sum(), then it would be referred to as Module1.sum() in our program.
When we issue from <module> import <object> command, internally following things take place:
- The code of imported module is interpreted and executed.
- Only the asked functions and variables from the module are made available to the program.
- No new namespace is created, the imported definition is just added in the current namespace.
- That means if your program already has a variable with the same name as the one imported via module, then the variable in your program will hide imported member with same name because there cannot be two variables with the same name in one namespace. Following code fragment illustrates this.