pytest-dev/pytest · error · AttributeError
module {__name__!r} has no attribute {name!r}
Error message
module {__name__!r} has no attribute {name!r} What it means
Raised by the module-level __getattr__ in _pytest.python when code accesses an attribute that does not exist on the module. It is the standard AttributeError produced for any unknown name on the _pytest.python module. Historically this also handles the deprecated CallSpec2 alias (emitting a warning then returning CallSpec); all other names raise this error.
Source
Thrown at src/_pytest/python.py:1842
style = "long"
return self._repr_failure_py(excinfo, style=style)
class FunctionDefinition(Function):
"""This class is a stop gap solution until we evolve to have actual function
definition nodes and manage to get rid of ``metafunc``."""
def runtest(self) -> None:
raise RuntimeError("function definitions are not supposed to be run as tests")
setup = runtest
def __getattr__(name: str) -> object:
if name == "CallSpec2":
warnings.warn(CALLSPEC2_RENAMED, stacklevel=2)
return CallSpec
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
View on GitHub (pinned to 98b357f69e)
Solutions
- Check the installed pytest version's _pytest/python.py for the actual exported names.
- Use the public pytest.* API instead of _pytest.python internals where possible.
- If importing CallSpec2, migrate to CallSpec (the alias emits a deprecation warning).
- Correct the typo in the import statement.
Example fix
// before from _pytest.python import CallSpec2 # deprecated/removed in future // after from _pytest.python import CallSpec
Defensive patterns
Strategy: validation
Validate before calling
import _pytest.python as pm
name = "CallSpec2"
if not hasattr(pm, name) and name != "CallSpec2":
raise AttributeError(f"_pytest.python has no attribute {name!r}")
# for CallSpec2, prefer CallSpec directly
from _pytest.python import CallSpec Type guard
import _pytest.python as pm
def module_has(name: str) -> bool:
return hasattr(pm, name) Try / catch
try:
from _pytest.python import CallSpec2 # deprecated
except (ImportError, AttributeError):
from _pytest.python import CallSpec as CallSpec2 Prevention
- Prefer the public pytest.* API over _pytest internals.
- Migrate off CallSpec2 to CallSpec.
- Verify internal names exist after upgrading pytest.
When it happens
Trigger: Doing `from _pytest.python import SomeName` or `_pytest.python.SomeName` where SomeName is not defined in the module (and is not the deprecated CallSpec2).
Common situations: Importing a private/internal name that was renamed or removed across pytest versions; typo in an import; depending on _pytest internals that are not part of the public API.
Related errors
- --pdbcls: could not import {value!r}: {exc}
- module or package not found: {arg} (missing __init__.py?)
- import error in {used}: {ex}
- {path}
- Can't find module {module_name} at location {path}
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/5ebbfd321fedd03c.json.
Report an issue: GitHub.