from
Updated March 17, 2026 · Keywords
keyword import modules
The from statement in Python imports specific names directly from a module into the current scope. This allows you to use imported names without the module prefix, making code more concise and readable.
Syntax
from module_name import name1, name2
from module_name import name as alias
from module_name import *
How It Works
When Python encounters a from statement:
- The specified module is loaded (if not already imported)
- The specified names are made available in the current scope
- You can use these names directly without the module prefix
Basic Import
from math import pi, sqrt
print(pi) # 3.141592653589793
print(sqrt(16)) # 4.0
Without the from statement, you would need to write:
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(16)) # 4.0
Import with Alias
You can rename imported items to avoid conflicts or for convenience:
from pandas import DataFrame as DF
from numpy import array as arr
Selective Import
Import only what you need to keep the namespace clean:
# Good: only import what you use
from os import path, makedirs
# Avoid: import everything (pollutes namespace)
from os import *
Common Patterns
Importing multiple names
from collections import defaultdict, Counter, OrderedDict
Relative imports (within packages)
from . import module_name
from ..package import module_name
from ..package.module import name
Re-exporting with alias
# In __init__.py
from .module import some_function as alias
Best Practices
- Use selective imports — avoid
from module import * - Use aliases for conflicts —
from module import long_name as short_name - Group imports — standard library, third-party, local (PEP 8)
- Keep imports at the top — except for conditional imports