How to import modules in Python


Python modules makes programming a lot easier.

It’s basically a file that consist of already written code.

When Python imports a module, it first checks the module registry (sys.modules)
to see if the module is already imported.

If that’s the case, Python uses the existing module object as is.

Importing Modules

There are different ways to import a module

import sys 
# access module, after this you can use to refer to
# things defined in module sys.

from sys import stdout 
# access module without qualifying name. 
# This reads >> from the module "sys" import "stdout", so that
# we would be able to refer "stdout" in our program.

from sys import * 
# access all functions/classes in the sys module.

Leave a Reply

Your email address will not be published. Required fields are marked *