Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Modules: Importing Modules

Lesson 39 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Modules — Importing Modules

A module is simply a .py file containing Python code (functions, classes, variables) that can be reused in other programs.

Ways to Import a Module

1. Import the whole module

import math
print(math.sqrt(16))    # 4.0
print(math.pi)            # 3.141592653589793

2. Import specific names

from math import sqrt, pi
print(sqrt(25))    # 5.0
print(pi)           # 3.141592653589793

3. Import with an alias

import math as m
print(m.factorial(5))   # 120

4. Import everything (generally discouraged)

from math import *
print(sqrt(9))   # 3.0 -- works, but pollutes the namespace

How Python finds modules

Python searches, in order:

  1. The current directory
  2. Directories in the PYTHONPATH environment variable
  3. The standard library installation directories
import sys
print(sys.path)   # list of directories Python searches for modules

dir() — list names defined in a module

import math
print(dir(math))   # lists all functions/constants available in the math module

Module caching

A module is only executed once per program run, even if imported multiple times (from different files); subsequent imports reuse the cached module object.

import sys
print('math' in sys.modules)   # False, before first import
import math
print('math' in sys.modules)   # True, after import