AttributeError: 'module' object has no attribute 'dumps'
AttributeError: 'module' object has no attribute 'dumps'
对于一个项目,我在 Python 中使用了 ROS-MQTT 网桥。这是从 ROS Wiki 获得的库。
该库可以在这里找到:https://github.com/groove-x/mqtt_bridge
其中一个 python 文件 (app.py) 使用以下代码行:
# load serializer and deserializer
serializer = params.get ('serializer', 'json: dumps')
deserializer = params.get ('deserializer', 'json: loads')
当运行roslaunch命令启动程序时,参考上面的代码出现如下错误:AttributeError: 'module' object has no attribute 'dumps'.
我查看了 this 问题,但答案对我的情况没有帮助。我在任何地方都找不到 json.py 文件,所以我也无法重命名它。如果我这样做 find sudo. -print | grep -i '. * [.] py'
我只会得到以下文件:
./app.py
./__init__.py
./bridge.py
./util.py
./mqtt_client.py
有兴趣者追溯:
Traceback (most recent call last):
File "/opt/ros/melodic/lib/mqtt_bridge/mqtt_bridge_node.py", line 9, in <module>
mqtt_bridge_node ()
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/app.py", line 50, in mqtt_bridge_node
mqtt_client, serializer, deserializer, mqtt_private_path)
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/app.py", line 15, in create_config
serializer = lookup_object (serializer)
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/util.py", line 12, in lookup_object
obj = getattr (module, obj_name)
AttributeError: 'module' object has no attribute 'dumps'
对于完整的 app.py 文件感兴趣的人:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import inject
import paho.mqtt.client as mqtt
import rospy
from .bridge import create_bridge
from .mqtt_client import create_private_path_extractor
from .util import lookup_object
def create_config(mqtt_client, serializer, deserializer, mqtt_private_path):
if isinstance(serializer, basestring):
serializer = lookup_object(serializer)
if isinstance(deserializer, basestring):
deserializer = lookup_object(deserializer)
private_path_extractor = create_private_path_extractor(mqtt_private_path)
def config(binder):
binder.bind('serializer', serializer)
binder.bind('deserializer', deserializer)
binder.bind(mqtt.Client, mqtt_client)
binder.bind('mqtt_private_path_extractor', private_path_extractor)
return config
def mqtt_bridge_node():
# init node
rospy.init_node('mqtt_bridge_node')
# load parameters
params = rospy.get_param("~", {})
mqtt_params = params.pop("mqtt", {})
conn_params = mqtt_params.pop("connection")
mqtt_private_path = mqtt_params.pop("private_path", "")
bridge_params = params.get("bridge", [])
# create mqtt client
mqtt_client_factory_name = rospy.get_param(
"~mqtt_client_factory", ".mqtt_client:default_mqtt_client_factory")
mqtt_client_factory = lookup_object(mqtt_client_factory_name)
mqtt_client = mqtt_client_factory(mqtt_params)
# load serializer and deserializer
serializer = params.get('serializer', 'json:dumps')
deserializer = params.get('deserializer', 'json:loads')
# dependency injection
config = create_config(
mqtt_client, serializer, deserializer, mqtt_private_path)
inject.configure(config)
# configure and connect to MQTT broker
mqtt_client.on_connect = _on_connect
mqtt_client.on_disconnect = _on_disconnect
mqtt_client.connect(**conn_params)
# configure bridges
bridges = []
for bridge_args in bridge_params:
bridges.append(create_bridge(**bridge_args))
# start MQTT loop
mqtt_client.loop_start()
# register shutdown callback and spin
rospy.on_shutdown(mqtt_client.disconnect)
rospy.on_shutdown(mqtt_client.loop_stop)
rospy.spin()
def _on_connect(client, userdata, flags, response_code):
rospy.loginfo('MQTT connected')
def _on_disconnect(client, userdata, response_code):
rospy.loginfo('MQTT disconnected')
__all__ = ['mqtt_bridge_node']
有人知道问题出在哪里吗?
经过 5 小时的尝试和研究后,我发现其中一个软件包依赖项丢失了。
该包正在检查缺少的依赖项,但那个除外。
通过在附加文件中添加一些印刷品,我发现它不是 json 丢失的。这是一个名为 msgpack 的依赖项。
执行 pip install msgpack-python==0.4.8
后我可以成功 运行 代码。
对于一个项目,我在 Python 中使用了 ROS-MQTT 网桥。这是从 ROS Wiki 获得的库。 该库可以在这里找到:https://github.com/groove-x/mqtt_bridge
其中一个 python 文件 (app.py) 使用以下代码行:
# load serializer and deserializer
serializer = params.get ('serializer', 'json: dumps')
deserializer = params.get ('deserializer', 'json: loads')
当运行roslaunch命令启动程序时,参考上面的代码出现如下错误:AttributeError: 'module' object has no attribute 'dumps'.
我查看了 this 问题,但答案对我的情况没有帮助。我在任何地方都找不到 json.py 文件,所以我也无法重命名它。如果我这样做 find sudo. -print | grep -i '. * [.] py'
我只会得到以下文件:
./app.py
./__init__.py
./bridge.py
./util.py
./mqtt_client.py
有兴趣者追溯:
Traceback (most recent call last):
File "/opt/ros/melodic/lib/mqtt_bridge/mqtt_bridge_node.py", line 9, in <module>
mqtt_bridge_node ()
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/app.py", line 50, in mqtt_bridge_node
mqtt_client, serializer, deserializer, mqtt_private_path)
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/app.py", line 15, in create_config
serializer = lookup_object (serializer)
File "/opt/ros/melodic/lib/python2.7/dist-packages/mqtt_bridge/util.py", line 12, in lookup_object
obj = getattr (module, obj_name)
AttributeError: 'module' object has no attribute 'dumps'
对于完整的 app.py 文件感兴趣的人:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import inject
import paho.mqtt.client as mqtt
import rospy
from .bridge import create_bridge
from .mqtt_client import create_private_path_extractor
from .util import lookup_object
def create_config(mqtt_client, serializer, deserializer, mqtt_private_path):
if isinstance(serializer, basestring):
serializer = lookup_object(serializer)
if isinstance(deserializer, basestring):
deserializer = lookup_object(deserializer)
private_path_extractor = create_private_path_extractor(mqtt_private_path)
def config(binder):
binder.bind('serializer', serializer)
binder.bind('deserializer', deserializer)
binder.bind(mqtt.Client, mqtt_client)
binder.bind('mqtt_private_path_extractor', private_path_extractor)
return config
def mqtt_bridge_node():
# init node
rospy.init_node('mqtt_bridge_node')
# load parameters
params = rospy.get_param("~", {})
mqtt_params = params.pop("mqtt", {})
conn_params = mqtt_params.pop("connection")
mqtt_private_path = mqtt_params.pop("private_path", "")
bridge_params = params.get("bridge", [])
# create mqtt client
mqtt_client_factory_name = rospy.get_param(
"~mqtt_client_factory", ".mqtt_client:default_mqtt_client_factory")
mqtt_client_factory = lookup_object(mqtt_client_factory_name)
mqtt_client = mqtt_client_factory(mqtt_params)
# load serializer and deserializer
serializer = params.get('serializer', 'json:dumps')
deserializer = params.get('deserializer', 'json:loads')
# dependency injection
config = create_config(
mqtt_client, serializer, deserializer, mqtt_private_path)
inject.configure(config)
# configure and connect to MQTT broker
mqtt_client.on_connect = _on_connect
mqtt_client.on_disconnect = _on_disconnect
mqtt_client.connect(**conn_params)
# configure bridges
bridges = []
for bridge_args in bridge_params:
bridges.append(create_bridge(**bridge_args))
# start MQTT loop
mqtt_client.loop_start()
# register shutdown callback and spin
rospy.on_shutdown(mqtt_client.disconnect)
rospy.on_shutdown(mqtt_client.loop_stop)
rospy.spin()
def _on_connect(client, userdata, flags, response_code):
rospy.loginfo('MQTT connected')
def _on_disconnect(client, userdata, response_code):
rospy.loginfo('MQTT disconnected')
__all__ = ['mqtt_bridge_node']
有人知道问题出在哪里吗?
经过 5 小时的尝试和研究后,我发现其中一个软件包依赖项丢失了。 该包正在检查缺少的依赖项,但那个除外。
通过在附加文件中添加一些印刷品,我发现它不是 json 丢失的。这是一个名为 msgpack 的依赖项。
执行 pip install msgpack-python==0.4.8
后我可以成功 运行 代码。