【问题标题】:AttributeError: 'module' object has no attribute 'dumps' (Checked for files)AttributeError:“模块”对象没有属性“转储”(检查文件)
【发布时间】:2017-07-29 14:20:03
【问题描述】:

我正在尝试使用 JSON 文件配置帐户,但它给出了 AttributeError。我检查了它正在导入的 JSON 的位置。这几天我一直在摆弄这个问题,我似乎找不到这个问题的根源。 JSON 中的__init__.py file 中定义了转储函数。我是 python 新手,所以如果我忽略了这个错误或者存在格式问题,我想提前道歉。谢谢。

输入:

import sys
import json
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855

__Author__ = 'Zach'

CLK = 11
DO = 25

sensor1 = MAX31855.MAX31855(CLK, 13, DO)
sensor2 = MAX31855.MAX31855(CLK, 29, DO)
sensor3 = MAX31855.MAX31855(CLK, 05, DO)
sensor4 = MAX31855.MAX31855(CLK, 06, DO)
sensor5 = MAX31855.MAX31855(CLK, 20, DO)
sensor6 = MAX31855.MAX31855(CLK, 21, DO)
sensor7 = MAX31855.MAX31855(CLK, 12, DO)
sensor8 = MAX31855.MAX31855(CLK, 16, DO)

try:
    config_data = open('/home/pi/TempMonitor/picloud_config.json').read()
    config = json.loads(config_data)
except:
    print 'Failed to load config data from picloud_config.json.  Exiting.'
    raise

host = config['aggregate_host']
room = config['room']
location = config['location']
api_key = config['api_key']
app_key = config['app_key']
dc = config['dc']

options = {
    'api_key': api_key,
    'app_key': app_key
}

tags = [
    'dc:' + dc,
    'room:' + room,
    'location:' + location
]

from datadog import initialize
from datadog import api
initialize(**options)

print('Press Ctrl-C to quit.')

print json
temp1 = float(sensor1.readTempC())
print('T1: {0:0.3F}*C'.format(temp1))
api.Metric.send(metric='sls.p7.temperature1', points=temp1, host=host, tags=tags)
temp2 = float(sensor2.readTempC())
print('T2: {0:0.3F}*C'.format(temp2))
api.Metric.send(metric='sls.p7.temperature2', points=temp2, host=host, tags=tags)
temp3 = float(sensor3.readTempC())
print('T3: {0:0.3F}*C'.format(temp3))
api.Metric.send(metric='sls.p7.temperature3', points=temp3, host=host, tags=tags)
temp4 = float(sensor4.readTempC())
print('T4: {0:0.3F}*C'.format(temp4))
api.Metric.send(metric='sls.p7.temperature4', points=temp4, host=host, tags=tags)
temp5 = float(sensor5.readTempC())
print('T5: {0:0.3F}*C'.format(temp5))
api.Metric.send(metric='sls.p7.temperature5', points=temp5, host=host, tags=tags)
temp6 = float(sensor6.readTempC())
print('T6: {0:0.3F}*C'.format(temp6))
api.Metric.send(metric='sls.p7.temperature6', points=temp6, host=host, tags=tags)
temp7 = float(sensor7.readTempC())
print('T7: {0:0.3F}*C'.format(temp7))
api.Metric.send(metric='sls.p7.temperature7', points=temp7, host=host, tags=tags)
temp8 = float(sensor8.readTempC())
print('T8: {0:0.3F}*C'.format(temp8))
api.Metric.send(metric='sls.p7.temperature8', points=temp8, host=host, tags=tags)

输出:

<module 'json' from '/usr/lib/python2.7/json/__init__.py'>
T1: NAN*C
Traceback (most recent call last):
  File "gather_temps.py", line 56, in <module>
    api.Metric.send(metric='sls.p7.temperature1', points=temp1, host=host, tags=tags)
  File "/usr/local/lib/python2.7/dist-packages/datadog/api/metrics.py", line 120, in send
    return super(Metric, cls).send(attach_host_name=True, **metrics_dict)
  File "/usr/local/lib/python2.7/dist-packages/datadog/api/resources.py", line 68, in send
    attach_host_name=attach_host_name)
  File "/usr/local/lib/python2.7/dist-packages/datadog/api/api_client.py", line 112, in submit
    body = json.dumps(body)
AttributeError: 'module' object has no attribute 'dumps'

【问题讨论】:

  • 我写成init.py,不知道为什么改了。这个也改了,但是init文件里有下划线
  • 对此类问题的通常解释是您在某处有一个名为 json.py 的文件,该文件在真正的 json 模块之前被发现。使用import json; print json.__file__ 查看它在哪里。
  • @jasonharper 感谢您检查我的问题。我确实检查了它,它给了我一个答案,说它正在使用定义的转储函数找到正确的文件。

标签: json python-2.7 import attributeerror


【解决方案1】:

我注意到的一件事是,您的输出表明 temp1 是 NAN,这意味着不是数字。我认为您的temp1 = float(sensor1.readTempC()) 出了点问题。修复此问题也可能会无意中修复您的 JSON 错误,因为 api.Metric.send 可能(很可能)假定输入是正确的(而不是 NAN)。

【讨论】:

  • NAN 只是我的温度传感器的读数。我插入它并首先运行它,但它遇到了同样的问题。如果我连接热电偶,它会给我一个数字,但没有热电偶它只是一个 NAN。
【解决方案2】:

所以问题是 Datadog 目录中有一些文件进入了错误的文件夹。经过一些卸载和重新安装目录。我让它工作得很好。

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多