Fix ImportError: cannot import name 'abs'
Quick answer
ImportError: cannot import name 'abs' almost always means a package's own internal import is failing — usually because a local file or stale .pyc shadows the real package, the install is broken or version-mismatched (classically TensorFlow with an incompatible protobuf), or there's a circular import. abs is a Python builtin, so you never import it yourself; find which package's import failed in the traceback and fix that.
ImportError: cannot import name 'abs' is confusing because abs() is a builtin you never import. The error isn't about your code importing abs — it's a package failing to load itself, and one of its internal imports (from ... import abs) fails along the way. The real question is always: which package is broken, and why.
Read the traceback first
The line that matters is the one naming the package whose import failed — often TensorFlow or one of its submodules. That tells you where to look. The message "cannot import name 'abs'" is a symptom of that package being shadowed, half-installed, or caught in a circular import.
The causes, and how to tell them apart
1. A local file shadows the real package (most common)
If you have a file named tensorflow.py, math_ops.py, numbers.py, or similar in your working directory — or a leftover __pycache__/.pyc — Python imports your file instead of the installed package, and the expected internals aren't there. Confirm it:
python -c "import tensorflow; print(tensorflow.__file__)"If that path is in your project directory instead of site-packages, you've found it.
2. A broken or version-mismatched install (the classic TensorFlow case)
A partial install, or a TensorFlow/protobuf version mismatch, leaves the package unable to complete its own imports. Reinstall cleanly, ideally in a fresh environment:
python -m venv .venv && source .venv/bin/activate
pip uninstall -y tensorflow protobuf
pip install "tensorflow==2.15.*" # a known-good, matched releaseUsing a virtualenv avoids the leftover-files problem that causes half of these errors.
3. A circular import
Module A imports B at the top, and B imports a name from A before A finishes loading. A is only partially initialized, so the name is missing. Break the cycle by importing inside the function that needs it, or by moving the shared symbol into a third module both can import.
4. A user-defined name shadowing the builtin
If your own module has abs = something or a file literally named abs.py, a later from mymodule import abs can pick up the wrong thing. Rename it — shadowing builtins is worth avoiding regardless.
Diagnose it in order
- Look at the traceback — which package's import failed?
- Check for shadowing:
print(pkg.__file__); look for stray.pyfiles matching package names in your cwd; delete__pycache__. - Check the install:
pip show <package>; reinstall in a clean venv if the version looks off. - Check for cycles: if it's your own package, look for two modules importing each other at load time.
Prevent it
- Never name a file after a package you import (
tensorflow.py,email.py,random.py). - Work inside a virtualenv so installs are clean and reproducible.
- Pin compatible versions for frameworks with tight internal coupling (TensorFlow + protobuf, for example).
- Clear caches after refactors: stale
.pycfiles cause "impossible" imports.
Once you identify which package is actually failing to import, the fix is almost always: remove the shadowing file, reinstall cleanly, or break the circular import.
Sources
Key takeaways
- •You are not importing abs — a package is importing it internally, and that package's load is broken. Read the traceback to see which one.
- •The #1 cause is shadowing: a local file named like the package (tensorflow.py, math_ops.py) or a stale __pycache__ makes Python load the wrong module.
- •Classic TensorFlow case: a partial or protobuf-mismatched install. Reinstall cleanly with pinned, compatible versions.
- •Check what actually got imported with `python -c "import pkg; print(pkg.__file__)"` — if it points at your own file or the cwd, that's the bug.
- •abs is a builtin. If your own variable or module is named abs, rename it — shadowing a builtin invites exactly this class of confusion.
Frequently asked questions
Do I need to import abs?
No. abs() is a Python builtin, always available without an import. This error never comes from your code trying to import abs — it comes from a library doing an internal `from ... import abs` while its own package is broken, shadowed, or half-loaded.
Why does this happen so often with TensorFlow?
Older TensorFlow releases did internal imports that broke when protobuf or the TF package itself was mismatched or partially installed. The fix is a clean reinstall with compatible versions: pip uninstall tensorflow protobuf, then pip install a known-good pair, in a fresh virtualenv.
How do I know if a local file is shadowing a package?
Run `python -c "import tensorflow; print(tensorflow.__file__)"` (swap in the failing package). If the path points at a file in your working directory instead of site-packages, you have a shadowing file — rename it and delete any __pycache__ folders and .pyc files.
What causes the circular-import version of this error?
Module A imports module B at the top level, and B imports a name from A before A has finished defining it. A is only partially initialized, so the name isn't there yet. Fix it by moving one import inside the function that needs it, or by restructuring so the two modules don't import each other at load time.
Software Engineering Leader & Technical Author · Updated August 31, 2026