Let's first understand What is a Module in Python & Why Import is needed - 

Module is a mean by which 'Code Modularization' in Python is being achieved.

In other words , each Module in Python is a 'Logical Grouping' of Functions , Classes & Variables which together achieve a specific purpose/objective.

Python has a predefined & pre-packaged Module named as '__builtins__' which has all the basic classes & Functions defined in it and it gets loaded to Python by default when we start Python.

To check which Modules are pre-loaded when you start Python , run the following script -
import sys

If you only want to know Classes & Functions loaded by default as part of Built-In , use following script -

But , are these pre-loaded functions / classes are enough to achieve everything we want to do with Python.
Most of the time , the answer is 'No' and that is when we look at other Modules available for Python .

Note - Python being an 'Open Source' , lots of experts & enthusiasts are working on developing new modules in Python and making the same available free for all to use .

One such Python Packages Repository is - PyPI . [ ]
The Python Package Index (PyPI) is a repository of software for the Python programming language.
PyPI helps you find and install software developed and shared by the Python community.

Now , lets discuss on How to leverage the Modules/Functions/Classes not pre-loaded by default into Python.

Before using any new Package , we have to Install those packages into your Python Environment [ Refer to the previous blogpost on how to install Packages ]

Once , Package has been installed , we have to import Module into Python before we can use the Functions/Classes available in that package.
[Not all installed Packages get imported by default as it will consume lots of memory space & thus , only the required Modules/Functions can be Imported selectively using the Import statement ]

3 flavors / types of Import are available in Python -

a) Import Module_Name

    Ex Code : Import sys 

With the above syntax , the Module sys gets technically binded to the name 'sys'
and now to access any functions within this module we have to prefix sys to it , ex - sys.exit()

b) from Module_Name Import object1 [,object2,object3...]

    Ex Code : from sys import exit

With the above syntax , only the specific objects from module (sys) gets loaded .
and now , the imported Objects (exit() ) can be directly called without prefixing with Module name (sys)

c) from Module_Name Import *

    Ex Code : from sys import *

It loads all of the names inside sys (like exit ) into Python instance. 
Now you can access all those names within sys without a prefix. ex - exit() instead of sys.exit()

Thank you & happy learning !!
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment