`import`
import loads a module, caches it, and binds names in the current namespace so you can use its functions, classes, and constants. from ... import ... binds selected names directly.
Detailed Explanation
import json does three jobs. It finds a module named json on sys.path. It executes that module if it is not already in sys.modules. It binds the module object to the name json in the current scope. After that, json.loads is an attribute lookup on the module.
import json as js only changes the local name. The module's __name__ is still json, and sys.modules["json"] is still the cache key. Aliases are for avoiding clashes and for long package paths, not for creating a second copy.
from json import loads still loads the whole module. Then it binds loads in the importer. It does not copy the function; it copies the reference. If the module later rebinds loads, your name still points at the old function. If the function mutates module-level state, you still share that state.
from json import * binds every public name listed in __all__, or every name that does not start with _. It makes it impossible to see where a name came from and can overwrite locals. Leave it for interactive sessions and for packages that exist to re-export an API.
Finding the file uses sys.path: the script's directory (or ""), then PYTHONPATH, then the standard library and site-packages. A package directory is searched for __init__.py or, for namespace packages, for portions on the path. The first match wins. Extension modules (.so, .pyd) and built-in modules participate in the same lookup.
Import is not a preprocessor include. The module's code runs once, in its own global namespace. Side effects at module level — connecting to a database, reading environment variables, registering plugins — run at first import. That is convenient and makes tests brittle if you are not ready for it.
Relative imports start with a dot and are resolved against __package__. from .tax import rate means "the tax sibling of this module". They fail when the file is run as a script. Absolute imports name the package from sys.path and are the default in application code.
Code Examples
import json
from pathlib import Path
import datetime as dt
payload = json.dumps({"when": dt.date(2026, 4, 1).isoformat()})
print(payload) # {"when": "2026-04-01"}
print(Path.cwd().name)# `from` binds a name, it does not snapshot a module forever.
from math import floor
print(floor(3.7)) # 3
# Rebinding the local name does not change math.floor.
floor = lambda x: int(x)
print(floor(3.7)) # 3, but this is no longer math.floorimport sys
print("json" in sys.modules) # True after the first import
# importlib.reload(json) re-runs the module; existing `from` names stay oldCommon Gotchas
Circular imports raise half-initialised modules
if A imports B while B imports A, one of them may see a module object that is missing attributes. Move shared names to a third module, or import inside the function.
`from module import name` is a snapshot of the binding
a later assignment in `module` is invisible. Import the module and use `module.name` when you need to see rebinds (rare) or when you want a stable prefix.
A local file can shadow the standard library
`json.py` in the current directory wins over the real `json`. Name project modules after the domain, not after stdlib.