Python “Module Has No Attribute”? A File in Your Folder Is Hiding the Real Module

Date:

Your script has run for months. Today it stops at import json with module 'json' has no attribute 'dumps'. json is part of Python itself; it can’t have lost dumps. So what changed?

The short answer: when a standard module has no attribute it always had, Python found a file called json in your script’s own folder before it found the real one. When you run python report.py, Python searches the folder that holds report.py first, ahead of its standard library, so a json.py you saved there, or a leftover json.pyc, wins. Find the file with python -c "import json; print(json.__file__)", then rename or delete it. To stop it happening at all, run scripts with python -P (Python 3.11 or newer), which leaves the script’s folder off the search path.

Module has no attribute: which case is yours?

What you seeLikely causeCheck it in one line
module 'json' has no attribute 'dumps' (any module name)A json.py beside your scriptpython -c "import json; print(json.__file__)"
The same error after you deleted json.pyA leftover json.pyc in the same folderls *.pyc in the script’s folder
cannot import name 'annotations' from '__future__'A __future__.py beside your scriptls __future__.py
No error, but the script prints something it never asked forA same-named file that defines what you import, so nothing failsRun with python -I and compare

Step 1: read the error to the end

On a test machine with Python 3.13, a folder holds report.py, which prints some JSON, and a notes file someone saved as json.py:

$ python report.py
Traceback (most recent call last):
  File "/home/you/app/report.py", line 2, in <module>
    print(json.dumps({"status": "ok"}))
          ^^^^^^^^^^
AttributeError: module 'json' has no attribute 'dumps' (consider renaming '/home/you/app/json.py' since it has the same name as the standard library module named 'json' and prevents importing that standard library module)Code language: JavaScript (javascript)

Python 3.13 names the file for you in the hint at the end. Older versions stop at has no attribute 'dumps', or say partially initialized module 'json' when the same-named file is the one importing it. Either way, the next step finds the file.

Step 2: ask Python which file it loaded

If there is no hint, ask:

$ python -c "import json; print(json.__file__)"
/home/you/app/json.pyCode language: JavaScript (javascript)

A path inside your project means your file is hiding the module. A path inside the Python install (.../lib/json/__init__.py) means it isn’t, and the attribute really is missing: check the spelling and the module’s version.

Why it happens. When you run a script, Python puts the script’s folder at the front of its module search path, ahead of the standard library. A file named after any module you import, json.py, random.py, email.py, test.py, gets loaded in its place. Your file has no dumps, so the error names an attribute rather than a missing module.

Step 3: you deleted the file and the error is still there

Look for a compiled copy. A json.pyc beside the script is imported exactly like json.py, even when json.py is gone:

$ ls
__pycache__  json.pyc  report.py
$ python report.py
AttributeError: module 'json' has no attribute 'dumps' (consider renaming '/home/you/app/json.pyc' ...)Code language: JavaScript (javascript)

Delete json.pyc and it runs. The copy inside __pycache__ is not the problem: Python only uses a __pycache__ file when its source file still exists, so __pycache__/json.cpython-313.pyc on its own did nothing on the test machine. A loose .pyc beside the script usually comes from an old Python version or from a tool that compiles files in place.

Step 4: the shadow that doesn’t raise an error

Same-named files don’t always crash. from __future__ import annotations looks like a compiler switch, but it is an ordinary import, and a __future__.py in the folder is loaded first. If that file happens to define the name, nothing fails, and the file’s own code runs before yours:

$ python tool.py
this ran first, from __future__.py in the same folder
tool ranCode language: JavaScript (javascript)

That line could have been anything the file’s author wanted. It matters when you run scripts from a folder other people or programs can write to, such as a shared folder, a downloads folder or an extracted archive: dropping a file there is enough to run code inside your script. Running it isolated shows the difference:

$ python -I tool.py
tool ran

Step 5: stop it for good

  1. Rename your file. Name your own modules after what they do (json_notes.py, my_random_tools.py), never after a standard library module. The module index lists the names to avoid.
  2. Run scripts with -P (Python 3.11 or newer). -P, or the PYTHONSAFEPATH=1 variable, leaves the script’s folder off the search path:

“ $ python -P report.py {"status": "ok"} $ PYTHONSAFEPATH=1 python report.py {"status": "ok"} “

If your script imports its own helper modules from its folder, -P breaks those imports. Install the project as a package, or import the helpers by path.

  1. Use -I for scripts that handle files you didn’t create. -I (isolated mode) does what -P does and also ignores PYTHON* environment variables and your user site-packages folder.
  2. Never run a script from inside a folder of downloaded or extracted files. Keep your scripts in their own folder and pass the other folder as an argument.

The short version

When a standard module suddenly “has no attribute”, a file in your script’s folder has the same name. python -c "import json; print(json.__file__)" shows which file Python loaded. Rename it, delete any loose .pyc beside it, and run scripts with python -P, or -I for anything that touches files from elsewhere. For other ways a script can depend on the machine it runs on, see why code works locally and fails on the server.

Frequently Asked Questions

Why does the same script work on another machine?

The other machine’s copy of the folder doesn’t contain the same-named file. Often it was never committed: a scratch json.py or test.py that lives only on one computer, or a .pyc left behind by an older Python. Compare the two folders, including hidden and compiled files.

Is it safe to delete __pycache__ folders?

Yes. Python rebuilds them the next time the modules are imported. Deleting them doesn’t fix this problem, though, because a __pycache__ file is ignored once its source is gone. Look for a loose .pyc beside the script.

Does a virtual environment protect me from this?

No. A virtual environment changes where installed packages come from, but the script’s own folder still comes first on the search path. Use -P or -I.

Why does the error mention “partially initialized module”?

Versions before 3.13 word it that way, often adding “most likely due to a circular import”. You ran a file whose own name matches the module it imports, for example json.py that starts with import json. Python is still loading your file when your file asks for json, so it gets your half-loaded file back. Python 3.13 reports the same case with the “consider renaming” hint instead. Rename the file.

Share post:

Subscribe

Popular

More like this
Related

Git Hook Not Running? Five Reasons It Does Nothing, and How to Tell Which

You added a pre-commit hook to block a mistake. You make the mistake on purpose to test it, and the commit goes through. No error, no output. Is the hook running at all?

Can AI CLI Conversation History Leak Answers Into a Blind Test?

You use an AI command-line tool to grade or answer hundreds of questions, one fresh run each. Then you find out it keeps every conversation on disk, answer keys included. Is run 200 quietly reading run 199?

The Code Snippets 403 That Comes From Your Host’s Firewall, Not WordPress

You change a few lines in a snippet, press Save, and get "Could not update snippet. Request failed with status code 403". Press Save on the unchanged snippet and it works. Your login is fine. Something between you and WordPress is reading the code itself.

Why a Flaky Test Passes Alone and Fails When the Machine Is Busy

Run the test by itself and it passes, every time. Run the whole suite in parallel and it fails, but only some days, and its failure looks exactly like a real bug. In our case the code was fine. The test was waiting on a clock.