simplejson json AttributeError: "module" object has no attribute "dump"
simplejson json AttributeError: "module" object has no attribute "dump"
我是编程新手,我有一个关于以下错误的问题。
我正在使用 python 2.7.,我有以下脚本来创建一个简单的图形(示例取自 Eric Matthes 的 python CrashCourse):
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares, linewitdth = 5)
#Set chart title and lable axes.
plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Value", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)
# Set size of tick labels
plt.tick_params(axis = "both", labelsize = 14)
plt.show()
当我在 WindowsPowerShell 中 运行 此脚本时,出现以下错误:
Traceback (most recent call last): File "mpl_squares.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\Roger\Anaconda2\lib\sitepackages\matplotlib\__init__.py, line 134, in <module> from ._version import get_versions
File "C:\Users\Roger\Anaconda2\lib\site-packages\matplotlib\_version.py", line 7, in <module> import json
File "C:\Users\Roger\Desktop\lpthw\json.py", line 7, in <module>
AttributeError: "module" object has no attribute "dump"
我在其他脚本中导入这个模块时遇到了同样的问题,然后我发现
通过将 "import json" 行替换为“import simplejson”的解决方案,效果很好。
这是我当时找到的解决方案:
json 很简单json,添加到标准库中。但是由于在 2.6 中添加了 json,因此 simplejson 具有在更多 Python 版本(2.4+)上工作的优势。
simplejson 也比 Python 更频繁地更新,所以如果您需要(或想要)最新版本,最好使用 simplejson 本身,如果可能的话。
在我看来,一个好的做法是使用一个或另一个作为后备。
尝试:将简单json导入为json
除了 ImportError: import json
现在我检查了错误,我得到了它指向的模块“_version.py”
这是此文件中包含的信息:
# This file was generated by 'versioneer.py' (0.15) from
# revision-control system data, or from the parent directory name of an
# unpacked source archive. Distribution tarballs contain a pre-generated
#copy
# of this file.
import json
import sys
version_json = '''
{
"dirty": false,
"error": null,
"full-revisionid": "26382a72ea234ee0efd40543c8ae4a30cffc4f0d",
"version": "1.5.3"
}
''' # END VERSION_JSON
def get_versions():
return json.loads(version_json)
问题:
你认为我必须通过替换来修复 _version.py 模块中的某些东西吗
import json for import simplejson 和模块中添加的函数?
我正在考虑解决问题的方法,但我不想修改 _version.py 中的任何内容,如果这会使事情变得更糟。非常感谢您的意见和建议。
此致
似乎导入了您的 C:\Users\Roger\Desktop\lpthw\json.py
而不是 Python 的 built-in json
模块。
您是否以某种方式将该文件夹 (C:\Users\Roger\Desktop\lpthw
) 添加到您的 PYTHONPATH
,例如使用 sys.path.append()
或 PYTHONPATH
变量?阅读更多关于 how Python finds modules.
使用 simplejson
进行修复的原因是它没有被其他同名模块覆盖。
尝试将 C:\Users\Roger\Desktop\lpthw\json.py
重命名为 C:\Users\Roger\Desktop\lpthw\myjson.py
之类的名称,并尝试弄清楚 lpthw
文件夹是如何进入您的 PYTHONPATH
.
的
我是编程新手,我有一个关于以下错误的问题。 我正在使用 python 2.7.,我有以下脚本来创建一个简单的图形(示例取自 Eric Matthes 的 python CrashCourse):
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares, linewitdth = 5)
#Set chart title and lable axes.
plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Value", fontsize = 14)
plt.ylabel("Square of Value", fontsize = 14)
# Set size of tick labels
plt.tick_params(axis = "both", labelsize = 14)
plt.show()
当我在 WindowsPowerShell 中 运行 此脚本时,出现以下错误:
Traceback (most recent call last): File "mpl_squares.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\Roger\Anaconda2\lib\sitepackages\matplotlib\__init__.py, line 134, in <module> from ._version import get_versions
File "C:\Users\Roger\Anaconda2\lib\site-packages\matplotlib\_version.py", line 7, in <module> import json
File "C:\Users\Roger\Desktop\lpthw\json.py", line 7, in <module>
AttributeError: "module" object has no attribute "dump"
我在其他脚本中导入这个模块时遇到了同样的问题,然后我发现 通过将 "import json" 行替换为“import simplejson”的解决方案,效果很好。
这是我当时找到的解决方案:
json 很简单json,添加到标准库中。但是由于在 2.6 中添加了 json,因此 simplejson 具有在更多 Python 版本(2.4+)上工作的优势。
simplejson 也比 Python 更频繁地更新,所以如果您需要(或想要)最新版本,最好使用 simplejson 本身,如果可能的话。
在我看来,一个好的做法是使用一个或另一个作为后备。
尝试:将简单json导入为json 除了 ImportError: import json
现在我检查了错误,我得到了它指向的模块“_version.py” 这是此文件中包含的信息:
# This file was generated by 'versioneer.py' (0.15) from
# revision-control system data, or from the parent directory name of an
# unpacked source archive. Distribution tarballs contain a pre-generated
#copy
# of this file.
import json
import sys
version_json = '''
{
"dirty": false,
"error": null,
"full-revisionid": "26382a72ea234ee0efd40543c8ae4a30cffc4f0d",
"version": "1.5.3"
}
''' # END VERSION_JSON
def get_versions():
return json.loads(version_json)
问题: 你认为我必须通过替换来修复 _version.py 模块中的某些东西吗 import json for import simplejson 和模块中添加的函数?
我正在考虑解决问题的方法,但我不想修改 _version.py 中的任何内容,如果这会使事情变得更糟。非常感谢您的意见和建议。
此致
似乎导入了您的 C:\Users\Roger\Desktop\lpthw\json.py
而不是 Python 的 built-in json
模块。
您是否以某种方式将该文件夹 (C:\Users\Roger\Desktop\lpthw
) 添加到您的 PYTHONPATH
,例如使用 sys.path.append()
或 PYTHONPATH
变量?阅读更多关于 how Python finds modules.
使用 simplejson
进行修复的原因是它没有被其他同名模块覆盖。
尝试将 C:\Users\Roger\Desktop\lpthw\json.py
重命名为 C:\Users\Roger\Desktop\lpthw\myjson.py
之类的名称,并尝试弄清楚 lpthw
文件夹是如何进入您的 PYTHONPATH
.