17 Modules in python
1. Any .py file is a module in python
2. Using module we can re use the code
# How to import a module ?
We can import in two ways
1. import
2. from import
# Difference between import and from import
1. Using import we can import entire module and
we have to use module name as prefix for each module attribute.
2. Using from import we can import required attribute from module and
no need to give prefix as module name to use module attribute.
1) import statement
'''
1. we can import a module using import keyword
'''
import base_module
# Print a variable from base_module
#print(base_module.names)
## call a add function from base_module
#base_module.add()
## call a sum function from base_module
#r = base_module.sum(20,50)
#print(' Sum value is :', r)
2) from import statement
'''
1. Import module attributes using "from" keyword
'''
## to import only names variable
#from base_module import names
## to import only name variable and add function
#from base_module import names, add
## to import all module attributes
#from base_module import *
## Print a variable from base_module
print(name)
## call a add function from base_module
#add()
## call a sum function from base_module
#r = sum(20,50)
#print(' Sum value is :', r)
3)import module from another path
'''
1. To import module from another path
we should add module path to sys.path list
2. sys is a python pre define module
'''
import sys
sys.path.append(r'C:\Users\LENOVO\Desktop\Testing using python\1-Python\6 modules\ONE')
import module2
module2.fun1()
####base_module.py
name = 'umamahesh'
names = ['sri','kumar','nagesh']
def add():
print(' Add is :', 3 + 4)
def sum(a,b):
return a + b
Comments
Post a Comment