Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

import

The import statement finds and initializes modules as needed, then binds modules or selected names in the current namespace. It supports aliases, qualified module paths, relative imports, and from ... import ... forms.

How it works

An import statement locates a module, loading and initializing it when needed, then creates bindings in the current scope. The basic form binds a top-level module name, while from imports bind selected attributes or submodules directly; as supplies an explicit alias.

Syntax

["lazy"] "import" module ["as" identifier] ("," module ["as" identifier])* | ["lazy"] "from" relative_module "import" identifier ["as" identifier] ("," identifier ["as" identifier])*

Parameters

NameTypeDescription
module / relative_module / identifiermodule name or identifierOne or more module paths, optionally followed by aliases or imported identifiers.

Returns

It binds imported modules or values in the current namespace; the statement itself has no user-visible return value.

Key points

  • Import both locates a module and creates a local binding for the result.
  • The basic form has separate lookup/loading and name-binding stages.
  • Importing can execute a module’s loading and initialization process.
  • Use as to choose the local name assigned to an imported module.
  • The from form places each successfully imported value into the current namespace.
  • The import statement is not the only interface for invoking Python’s import machinery.

Common mistakes

  • Wildcard imports cannot be used inside class or function definitions.
  • A from import fails with ImportError when the requested attribute or submodule cannot be found.
  • Importing a nested module with the basic form may bind its top-level package, so access the nested module through its qualified name.

Example: Import modules and names

import math
from pathlib import Path as FilePath

print(math.sqrt(16))
print(FilePath('.').resolve())

This imports the math module under its original name and binds Path under the alias FilePath.

When to use it

Load a module

Reuse functionality implemented in another module or package.

Create an alias

Expose a shorter or clearer local name for a module or imported value.

Import specific names

Bring selected functions, classes, or constants into the current namespace.

Use relative imports

Load a package-relative module within a package hierarchy.

Interview questions

QWhat are the two conceptual steps of an import?

The statement first finds the requested module, loading and initializing it if necessary. It then binds the resulting module or imported values in the current namespace.

Follow-up: How does from package import name resolve name? It checks the package for an attribute, then may try a submodule and checks again before raising ImportError.

QHow do import and from ... import ... differ?

import package.module normally binds the top-level package name, so the nested module is accessed through its qualified name. from package.module import name instead stores the selected value directly in the current namespace.

Follow-up: What changes when as alias is used? The alias becomes the binding name for the imported module or value.

QWhat does the as clause do?

An alias gives the imported module or value an explicit local binding. Without an alias, a top-level module normally binds its own name; with as, the name after as is bound directly.

Follow-up: Where is wildcard import permitted? Only at module level, not inside class or function definitions.

  • importlib.import_module()
  • __import__()
  • module
  • relative_module
  • ImportError

Source: 7. Simple statements. Checked 2026-10-16.