Skip to content
Home » Import command

Import command

Here is a clear, complete, and exam-ready explanation of the import command in Python, suitable for BCA/MCA/B.Tech students.


Import Command in Python

The import command in Python is used to include modules (libraries) in your program.
A module is a file containing Python code — functions, variables, or classes — that can be reused in different programs.

Using import, you can access thousands of built-in and external Python modules such as:

  • math
  • random
  • datetime
  • os
  • sys
  • pandas
  • numpy
    and many more.

1. Why Do We Use import?

import allows you to:

✔ Reuse code written by others
✔ Reduce development time
✔ Access powerful functionalities
✔ Organize code into modules

Example:

import math
print(math.sqrt(25))

2. Basic Use of import

Syntax:

import module_name

Example:

import random
print(random.randint(1, 10))

Here, we imported the random module and called its randint() function.


3. Importing Specific Functions from a Module

Syntax:

from module_name import function_name

Example:

from math import sqrt
print(sqrt(16))

Here:

  • Only the sqrt function is imported, not the entire module.
  • We can directly use it without math. prefix.

4. Importing Multiple Functions

Syntax:

from module_name import function1, function2, function3

Example:

from math import sin, cos, tan

5. Importing All Functions (Wildcard Import)

Syntax:

from module_name import *

Example:

from math import *
print(sqrt(9))

Note:
This is not recommended in large programs because:

  • It becomes confusing (too many functions imported)
  • Function name conflicts may occur

6. Importing a Module with an Alias (Nick Name)

Syntax:

import module_name as alias

Example:

import numpy as np

Here:

  • numpy is imported as np
  • easier to write functions like np.array()

Another example:

import math as m
print(m.pi)

7. Using from module import function as alias

Example:

from math import factorial as fact
print(fact(5))

8. Importing User-Defined Modules

If you create your own Python file (module), you can import it in another file.

Example:

file1.py

def greet():
    print("Hello from file1")

file2.py

import file1
file1.greet()

This is how Python supports modular programming.


9. Importing Modules from Folders (Packages)

You can organize modules into folders called packages.

Example structure:

myproject/
    pkg/
        __init__.py
        module1.py
        module2.py

Importing:

from pkg import module1
module1.function()

10. Reloading Modules (Advanced)

If you modify a module while executing the program, use:

import importlib
importlib.reload(module_name)

11. Types of Imports (Summary Table)

TypeSyntaxExample
Import entire moduleimport moduleimport math
Import specific functionfrom module import funcfrom math import sqrt
Import all functionsfrom module import *from math import *
Import with aliasimport module as aliasimport numpy as np
Function aliasfrom module import func as aliasfrom math import factorial as fact

12. Common Errors with Import

1. ModuleNotFoundError

ModuleNotFoundError: No module named 'abcd'

Occurs when:

  • module name is wrong
  • module not installed

2. ImportError

Occurs when:

  • specific function not found inside module

Conclusion

The import command is essential in Python because it allows you to use predefined functions, external libraries, and modular code. It improves productivity, readability, and organization of Python programs.