Python Module

Introduction

In this article, you will learn about Python modules, which are the files, which contains Python code, modules can contain functions, classes and variables.
It is an executable code.

Import statement

Import statement is used to import a module in our Python program.

Syntax-

import module_name

Example-
  1. import math  
  2. n=-5  
  3. print("abslut value is: ",abs(n))  
Output-
from import statement

In Python “from statement” is used to import the particular attributes from a module.

Syntax-

from module_name import name

Example-
  1. from math import cos  
  2. n=30  
  3. print("cosine value:",cos(n))  
Output-
 
from import *

It is used to import an entire name from the module into a current program.

Syntax-

From module_name import*

Example-
  1. from math import*  
  2. n=-30  
  3. print("abs value:",abs(n))  
Output-
 
PYTHONPATH Variable

It is an environment.

It consists of a list of directory and it is also used to set directory of Python.

Set PYTHONPATH=c:\python3\lib;

dir() function

dir() function returns the list of all the names from the module.

Syntax-

dir(module_name)

Example-
  1. import math  
  2. print(dir(math))  
Output-
 
globals () and locals() function

globals()

The globals function can be used to return all the names, which are globally defined.

Locals()

locals() function is used to return all the name. They can be accessed locally.

reload function()

It reloads all the module import in Python program.

Summary

In this chapter, you learnt what module is, how to import module into our Python program and how to used predefine function of module