1

I'm trying to load JSON from a file into python, but I keep receiving the error:

AttributeError: module 'json' has no attribute 'load'

I don't have files called json.py in my directory like other answers suggested.

When I do:

import json;
print(json.__file__)
/usr/lib/python3.6/json/__init__.py

I receive:

/usr/lib/python3.6/json/init.py

I have checked the file and it does contain the methods.

The full script is:

#!/usr/bin/env python3

import json;
print(json.__file__)
import sys;


#a = json.load('["foo", {"bar":["baz", null, 1.0, 2]}]')
#pprint(a);

#sys.exit();

from pprint import pprint;

with open('services.json') as f:
        data=json.load(f);

pprint(data);

I'm kinda losing my mind here. When I change the shebang to python2.7 it does work, however, I have written my other scripts with python3.6.

Help would be very much appreciated!

Solution: I named the file 'enum.py', changing the name removes the error.

6
  • No, that's the strange thing. I stated that in my question by the way, I have a couple files, the only thing that comes close is 'services.json' which is the actual file I want to load. Commented Jun 13, 2018 at 17:59
  • 1
    Use the python3 interpreter and try from there. Commented Jun 13, 2018 at 18:08
  • 1
    Make sure you don't have some other object called json in the program, like a function or variable. I guess it would have to be a module... try dir(json) to see if load is in the list. Commented Jun 13, 2018 at 18:19
  • @musikreck When I try to use python3.6 from a shell in another directory, it works. However, when I load it in the directory and try to import json I get the same error. The only files I have are: pycache auto-recon.py enum.py services.json threads.py. Any idea what might be the cause of this? Commented Jun 13, 2018 at 18:24
  • @jeffpkamp The code above is everything, it's not being imported currently. The error happens based on running the file solely. dir(json): ['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', 'all', 'author', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'version', '_default_decoder', '_default_encoder', 'codecs', 'decoder', 'detect_encoding', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner'] Commented Jun 13, 2018 at 18:27

1 Answer 1

1

just use simplejson lib. worked for me

import simplejson
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.