🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

import Statement

AI & DATA SCIENCE // import-statement

The import statement loads a module (a .py file, or a package) and makes its contents accessible through a namespace named after the module.

Syntax

import math
import math as m
import pkg.submodule

Deep Dive Course

Writing import module_name runs that module's code once, the first time it's imported in a given program, since subsequent imports reuse the already-loaded, cached module instead of re-running it, and binds the module itself as an object to a name in the current scope. Everything inside the module — functions, classes, variables — is then accessed through attribute access on that name, keeping the importing code's own namespace uncluttered by everything the module defines.

1Understanding import Statement

Writing import module_name runs that module's code once, the first time it's imported in a given program, since subsequent imports reuse the already-loaded, cached module instead of re-running it, and binds the module itself as an object to a name in the current scope. Everything inside the module — functions, classes, variables — is then accessed through attribute access on that name, keeping the importing code's own namespace uncluttered by everything the module defines.

💡

Python caches every imported module after its first import — this is why import statements are cheap to repeat across many files, and why top-level module code, like print statements or expensive setup, only actually runs once per program, no matter how many places import it.

editor.html
import math
print(math.sqrt(16))
print(math.pi)
localhost:3000

2Practical Example

Here is a real-world application of import Statement showing how it is used in production Python code.

editor.html
import random
random.seed(42)
print(random.randint(1, 100))
localhost:3000

3Best Practices

Follow these guidelines when working with import Statement:

1. Use import module and access things through the module name for clarity about where a name comes from, unless a specific name is used extremely often

2. Put all imports at the top of a file, grouped by standard library, third-party, and local imports, per PEP 8

3. Avoid import *, which pulls an unpredictable set of names into your namespace and makes it unclear where any given name actually came from

⚠️

Tip: Python caches every imported module after its first import — this is why import statements are cheap to repeat across many files, and why top-level module code, like print statements or expensive setup, only actually runs once per program, no matter how many places import it.

editor.html
import math
print(math.sqrt(16))
print(math.pi)
localhost:3000

Examples

Example 01Basic Usage
import math
print(math.sqrt(16))
print(math.pi)
Example 02Advanced Example
import random
random.seed(42)
print(random.randint(1, 100))

Best Practices

  • Use import module and access things through the module name for clarity about where a name comes from, unless a specific name is used extremely often
  • Put all imports at the top of a file, grouped by standard library, third-party, and local imports, per PEP 8
  • Avoid import *, which pulls an unpredictable set of names into your namespace and makes it unclear where any given name actually came from

Interview Question

Why does importing the same module in ten different files only actually execute that module's top-level code once?

Hint: Think about how Python tracks which modules have already been loaded.

Python keeps a cache of every module it has already imported, keyed by module name. The first time a module is imported anywhere in a running program, Python executes its top-level code and stores the resulting module object in that cache; every subsequent import of the same module, from any file, simply looks it up in the cache and reuses the existing object instead of re-running the module's code from scratch.

Exercises

MediumPractice using import Statement in a real scenario.
View Solution
import math
print(math.sqrt(16))
print(math.pi)

Frequently Asked Questions

Why does importing the same module in ten different files only actually execute that module's top-level code once?

Python keeps a cache of every module it has already imported, keyed by module name. The first time a module is imported anywhere in a running program, Python executes its top-level code and stores the resulting module object in that cache; every subsequent import of the same module, from any file, simply looks it up in the cache and reuses the existing object instead of re-running the module's code from scratch.

Related Functions

from-importas-keywordpip