python3.x - python错误:module 'json' has no attribute 'load'
迷茫
迷茫 2017-04-18 09:34:22
0
2
1799

Macbook 今早唤醒,执行下面的几行简单的代码,报错找不到load方法。

import json

# add json data into db
json_path = '/Users/X/Desktop/keywords/ieee_keywords.json'

with open(json_path) as f:
    data =json.load(f)
print(data)
Traceback (most recent call last):
  File "json.py", line 2, in <module>
    import json
  File "/Users/X/Desktop/crawl_keywords/json.py", line 12, in <module>
    data =json.load(f)
AttributeError: module 'json' has no attribute 'load'

这是咋回事,我又尝试新建了一个文件夹,用virtualenv开个虚拟环境,还是这个错误。

甚至我在虚拟环境里试图使用pip install flask

Traceback (most recent call last):
  File "/Users/Ru/Desktop/crawl_keywords/py3/bin/pip", line 7, in <module>
    from pip import main
  File "/Users/Ru/Desktop/crawl_keywords/py3/lib/python3.5/site-packages/pip/__init__.py", line 16, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/Users/Ru/Desktop/crawl_keywords/py3/lib/python3.5/site-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/Users/Ru/Desktop/crawl_keywords/py3/lib/python3.5/site-packages/pip/download.py", line 6, in <module>
    import json
  File "/Users/Ru/Desktop/crawl_keywords/json.py", line 10, in <module>
    data =json.load(f)
AttributeError: module 'json' has no attribute 'load'

其他包同样的,例如psycopg2找不到connect等。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

Antworte allen(2)
迷茫

因为某种原因导致导入 json 时,没有导入 python 内置的模块,而是导入了你本地的json.py 文件!!

File "/Users/X/Desktop/crawl_keywords/json.py", line 12, in <module>
...
File "/Users/Ru/Desktop/crawl_keywords/json.py", line 10, in <module>

可以看看 https://github.com/xuelangZF/... ,了解下包导入机制

阿神

单词少写了,json.loads,不是load

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage