Untitled Document
9 Importing Modules - Exploring Standard Library
1. Simple Example to Demo the Importing Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****  
       # Simple Example to Demo the Importing    
     
      # Here we will create  two python files
      # 1. intro.py
      # 2. my_module.py

# intor.py will import my_module and we will actually execute intro.py 

 Intro.py file 

 import my_module
 courses = ['History','Math', 'Physics', 'Sanskrit']
 my_module.find_index(courses, 'Sanskrit')

 my_module.find_index(courses, 'Sanskrit12')

# You can import my_module and give it it short name or alias like mm
# so instead of giving - my_module.find_index(courses, 'Sanskrit')
# You can write mm.find_index(courses, 'Sanskrit')

# You can also import only specific find_index function ; but then you won't be able to access any other varibale 
# or method from that module 

from my_module import find_index 
# then you don't have to pass the module name while using find_index method
find_index(courses, 'Sanskrit12')
       
2. Importing Few more elemnets Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Importing Few  more elemnets
# In Below example along with find_index method we will import test varibale 

from my_module import find_index, test 

courses = ['History','Math', 'Physics', 'Sanskrit']
print(find_index(courses, 'Sanskrit'))
print(test)
       
3. Importing Everything from my_module script using * Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       #  Importing Everything from my_module script using *
       # * gives access to all the variables and methods from my_module
       # IMP - though * provide access to everything from importing module ; but when we have issues or error its
       # hard to debug ; from where the issues has came on 
       # And its even hard to check what all has been imported from this module 

from my_module import *

courses = ['History','Math', 'Physics', 'Sanskrit']
print(find_index(courses, 'Sanskrit'))
print(test)
       
4. How Python Searches the Module ; even though we didn't specify any path Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How Python Searches the Module ; even though we didn't specify any path
       # To get the answer of above questions lets import sys module 

import sys 
print(sys.path)

# The sys.path contains all the paths that are used by python while importing modules 
# First it check in your local path i.e the path from where we are executing script 
# Second it then checks paths added to Env. variables 
# Then it checks standard libraries i.e in built modules 
# and lastly it checks any third party installed packages 

       
5. Try to import different modules from standard library modules Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Try to import different modules from standard library modules
       
       # like  - sys, datetime , calendar , random , os etc.. 
       # we have seprarte chapters for these modules and you can refer the examples from there.

       # IMP if you want to get the file path or exact python file name of standard library modules 
       # then simply run below code ; this is for os module but in case you want for any other modules 
       # then just replace os module with any other module 

import os 
print(os.__file__)
       
      
       
Untitled Document