Python Modules | Creating Modules and Importing Modules In Python MSBTE
Python Modules | Creating Modules and Importing Modules In Python MSBTE |
Python Modules
- Previous sections were all about defining functions and using them. In this section we will discuss about Python modules, for the purpose of re-usability again.
- A module is a file, containing functions ,classes and global variables.
- A module can also include runnable code.
- Grouping related code into a module makes the code easier to understand and use it by using simple import statement.
Writing Modules
- We don't require any additional effort or syntax of keyword to define a module. Just save your code in a Python with extension .py. To begin this discussion, let's start with a very easy module.
- def cw_Add(no1,no2):
return no1+no2
def cw_Sub(no1,no2):
return no1-no2
def cw_Div(no1,no2):
return no1/no2
def cw_Multiply(no1,no2):
return no1*no2
def cw_Power(no1,no2):
return no1**no2 - Let's save above code as "cwpedia_operations.py" ,After saving above code, the file cwpedia_operations will behave as a module and can be imported anytime by using simple import statement.
Importing Modules
- After defining required module, now it is time to use it in other Python file or other Python module. Use keyword import to do this. Following sub-sections shows different ways and purposes to import and use a module.
Simple import Statement
- We can import module using import statement and access its entities by using the dot operator. To import, general syntax is :
- import module_name
- Note that this simple import statement will not make the entities of modules directly accessible. Technically, it will only create a separate namespace and link it with current Python file. To use the entities of imported module, we have use following syntax
- module_name.entity_name
- Here is a Python file that imports above module cwpedia_operations and uses its entities.
- import cwpedia_operations
n1=5
n2=2
print("Addition Of ",n1,"and",n2,"is",cwpedia_operations.cw_Add(n1,n2))
print("Subtraction Of ",n1,"and",n2,"is",cwpedia_operations.cw_Sub(n1,n2))
print("Division Of ",n1,"and",n2,"is",cwpedia_operations.cw_Div(n1,n2))
print("Multiplication Of ",n1,"and",n2,"is",cwpedia_operations.cw_Multiply(n1,n2))
print(n1,"raised to",n2,"is",cwpedia_operations.cw_Power(n1,n2)) - Let’s call above Python file as “testing module.py". Note the statements from line-4 to line-8, The functions defined in module cwpedia_operations are called and used by using dot operator (.)
- Even if the module contains user-defined classes or any global declarations, we have to use them by using module_name and dot operator. Above code will show following output.
- Output:
- Addition Of 5 and 2 is 7
- Subtraction Of 5 and 2 is 3
- Division Of 5 and 2 is 2.5
- Multiplication Of 5 and 2 is 10
- 5 raised to 2 is 25
Import with Renaming
- Note that in above testing_module.py line-1 imports the module cwpedia_operations and used its functions. But if the name of a module is too large, then we can also import an entire module under an alternate name. To do this, syntax is:
- import module name as alt_name
- Use the keyword 'as' to rename it. Here is an alternate of above testing module.py with renamed module
- import cwpedia_operations as a
n1=5
n2=2
print("Addition Of ",n1,"and",n2,"is",a.cw_Add(n1,n2))
print("Subtraction Of ",n1,"and",n2,"is",a.cw_Sub(n1,n2))
print("Division Of ",n1,"and",n2,"is",a.cw_Div(n1,n2))
print("Multiplication Of ",n1,"and",n2,"is",a.cw_Multiply(n1,n2))
print(n1,"raised to",n2,"is",a.cw_Power(n1,n2)) - Too much easy and short name 'a' is used for module cwpedia_operations . Also note the function calling statements from line-4 to line-8. They are also using new name 'a' to call module's functions. We can use any name to rename a module, but prefer to make it short and easy, as we did in above example.
The from...import Statement OR Importing Objects from Modules OR Importing Individual Object
- It is also possible to import specific attribute/member from a module without importing the module as a whole. The from….import has the following syntax:
- from module_name import name-1, name-2……..name-n
- For example, if we want to import only multiplication function from module cwpedia_operations , then it can be done and used as follows:
- from cwpedia_operations import cw_Multiply
print("Multiplication of 5 and 6 is:"cw_Multiply(8,7)) - Note that, in line-1 only cw_Multiply () is imported from module cwpedia_operations . And line-2 shows the calling and use of imported cw_Multiply () function. The difference between this type of import statement and previous import statements can be seen in line-2, the way how cw_Multiply () were called. Also read and understand following statement.
- from cwpedia_operations import cw_Multiply,cw_Power
- As a conclusion, Python's from…...import statement lets us import specific attribute(s) from a module into the current namespace, and makes its part/member of current namespace.
Import everything using *
- You can import all the members/attributes of the module into the namespace your working, using the following import statement:
- from module_name import *
- The Statement provides us an easiest way to import all of the members/functions in a module into the namespace you are working: however, this statement should be used carefully to avoid any cause. To import all module functions / members cwpedia_operations ,we have following example.
- cwpedia_operations import *
n1=5
n2=2
print("Addition Of ",n1,"and",n2,"is",cw_Add(n1,n2))
print("Subtraction Of ",n1,"and",n2,"is",cw_Sub(n1,n2))
print("Division Of ",n1,"and",n2,"is",cw_Div(n1,n2))
print("Multiplication Of ",n1,"and",n2,"is",cw_Multiply(n1,n2))
print(n1,"raised to",n2,"is",cw_Power(n1,n2)) - This will show following output:
- Addition Of 5 and 2 is 7
- Subtraction Of 5 and 2 is 3
- Division Of 5 and 2 is 2.5
- Multiplication Of 5 and 2 is 10
- 5 raised to 2 is 25
Post a Comment