AttributeError: module has no attribute (tested)

AttributeError: module 'X' has no attribute 'Y' has four common causes. We reproduced each on Python 3.13.5, including one where the error's own hint points at the wrong problem.

1. A submodule that was never imported

python3 -c "import xml; xml.etree" 2>&1 | tail -1
python3 -c "import xml.etree.ElementTree; import xml; print(xml.etree.ElementTree.fromstring('<a/>').tag)"

Output:

AttributeError: module 'xml' has no attribute 'etree'
a

Measured on Python 3.13.5: import xml does not load xml.etree; a package's submodules only become attributes once something imports them. Import the full path you use (import xml.etree.ElementTree). It's also why code can work only when some other module happened to import the submodule first.

2. Your file has the same name as the module

mkdir -p d1 && cd d1 && echo "x = 1" > random.py
python3 -c "import random; random.randint(1, 2)" 2>&1 | tail -1

Output:

AttributeError: module 'random' has no attribute 'randint' (consider renaming '/tmp/mh2/d1/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)

Measured: a random.py in the current directory is imported instead of the standard library's random. Python 3.13 names the file and says it shadows the standard library. Rename the file and delete any __pycache__ next to it.

3. A circular import, with a misleading hint

mkdir -p d2 && cd d2
printf 'import b\ndef f():\n    return "f"\n' > a.py
printf 'import a\na.f()\n' > b.py
python3 -c "import a" 2>&1 | tail -1

Output:

AttributeError: module 'a' has no attribute 'f' (consider renaming '/tmp/mh2/d2/a.py' if it has the same name as a library you intended to import)

Measured: a imports b, which calls a.f() while a is still on its first line, so f doesn't exist yet. Python 3.13 suggests renaming a.py, but nothing is being shadowed. If the traceback shows two of your own files importing each other, it's circular: move the call into a function, or move shared code into a third module. More in our cannot import name guide.

4. A typo or a renamed function

python3 -c "import math; math.sqroot(4)" 2>&1 | tail -1

Output:

AttributeError: module 'math' has no attribute 'sqroot'. Did you mean: 'sqrt'?

Measured: typos get a Did you mean suggestion. With no suggestion, check whether the library version you have installed still has that name (print(dir(module)) or pip show).

Frequently asked questions

What does AttributeError: module has no attribute mean?

The module object you got doesn't have that name. We measured four causes on Python 3.13.5: an unimported submodule, a local file shadowing the module, a circular import, and a typo.

Why does import xml not give me xml.etree?

Importing a package doesn't import its submodules. We measured xml.etree failing after import xml and working after import xml.etree.ElementTree.

Why does my random.py break import random?

Python imports your file instead of the standard library. We measured the 3.13 hint naming the file; rename it.

Can a circular import cause module has no attribute?

Yes. We measured two files importing each other giving this error, with a misleading 'consider renaming' hint on Python 3.13.5.

Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5: import xml vs import xml.etree.ElementTree, a local random.py, a two-file circular import, and a typo in math. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.