🚀 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...

from ... import

AI & DATA SCIENCE // from-import

The from ... import statement pulls specific names directly out of a module into the current namespace, so they can be used without the module prefix.

Syntax

from math import sqrt, pi
from collections import defaultdict
from package.module import ClassName

Deep Dive Course

Instead of importing an entire module and accessing everything through its name, this form binds specific names directly into the current scope, so you can call a function directly instead of prefixing it with the module name each time. It still runs the whole module's top-level code the first time, and caches it the same way a plain import does — it just changes which names end up directly accessible without a prefix. Using a wildcard imports every public name the module exposes, which is generally discouraged since it makes it unclear where any given name in your code actually came from.

1Understanding from ... import

Instead of importing an entire module and accessing everything through its name, this form binds specific names directly into the current scope, so you can call a function directly instead of prefixing it with the module name each time. It still runs the whole module's top-level code the first time, and caches it the same way a plain import does — it just changes which names end up directly accessible without a prefix. Using a wildcard imports every public name the module exposes, which is generally discouraged since it makes it unclear where any given name in your code actually came from.

💡

Avoid a wildcard from-import in real code — it silently pulls in an unpredictable set of names, can shadow existing names in your file without warning, and makes it much harder for a reader, or an IDE, to tell where a given name is defined.

editor.html
from math import sqrt, pi
print(sqrt(25))
print(pi)
localhost:3000

2Practical Example

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

editor.html
from collections import defaultdict
counts = defaultdict(int)
for word in ["a", "b", "a", "c", "a"]:
    counts[word] += 1
print(dict(counts))
localhost:3000

3Best Practices

Follow these guidelines when working with from ... import:

1. Use from module import specific_name for names you use frequently, to avoid repeating the module prefix everywhere

2. Avoid a wildcard from-import, since it obscures where each imported name actually comes from and risks silently overwriting existing names

3. List multiple imported names from the same module in one from-import statement instead of repeating separate import lines

⚠️

Tip: Avoid a wildcard from-import in real code — it silently pulls in an unpredictable set of names, can shadow existing names in your file without warning, and makes it much harder for a reader, or an IDE, to tell where a given name is defined.

editor.html
from math import sqrt, pi
print(sqrt(25))
print(pi)
localhost:3000

Examples

Example 01Basic Usage
from math import sqrt, pi
print(sqrt(25))
print(pi)
Example 02Advanced Example
from collections import defaultdict
counts = defaultdict(int)
for word in ["a", "b", "a", "c", "a"]:
    counts[word] += 1
print(dict(counts))

Best Practices

  • Use from module import specific_name for names you use frequently, to avoid repeating the module prefix everywhere
  • Avoid a wildcard from-import, since it obscures where each imported name actually comes from and risks silently overwriting existing names
  • List multiple imported names from the same module in one from-import statement instead of repeating separate import lines

Interview Question

Why is a wildcard from-import generally discouraged compared to importing specific names?

Hint: Think about readability and the risk of silently overwriting existing names.

A wildcard from-import pulls in every public name the module defines, without listing them explicitly, so a reader looking at code that uses one of those names has no easy way to tell which module it actually came from, and tools like linters and IDEs also struggle to resolve it. It can also silently shadow an existing name already defined in your file, or imported from another module, creating confusing bugs that only show up when the wildcard-imported module happens to define a conflicting name.

Exercises

MediumPractice using from ... import in a real scenario.
View Solution
from math import sqrt, pi
print(sqrt(25))
print(pi)

Frequently Asked Questions

Why is a wildcard from-import generally discouraged compared to importing specific names?

A wildcard from-import pulls in every public name the module defines, without listing them explicitly, so a reader looking at code that uses one of those names has no easy way to tell which module it actually came from, and tools like linters and IDEs also struggle to resolve it. It can also silently shadow an existing name already defined in your file, or imported from another module, creating confusing bugs that only show up when the wildcard-imported module happens to define a conflicting name.

Related Functions

import-statementas-keywordname-main