Can't run Python nodes: ImportError: cannot import name 'String' from 'std_msgs.msg'

asked 2023-02-08 07:24:26 -0500

CharlesPavlov gravatar image

updated 2023-02-09 04:17:15 -0500

Hi! I'm running into issues preventing me from running python nodes on ROS2 Humble. I was following the "ROS2 Robotics Developer Course - Using ROS2 In Python" course on Udemy, when I started running into the following error preventing me from running python nodes:

ImportError: cannot import name 'String' from 'std_msgs.msg' (/opt/ros/humble/local/lib/python3.10/dist-packages/std_msgs/msg/__init__.py)

The error occurs whenever I try to run any note written in python, for example:

$ ros2 run demo_nodes_py listener

Note the following:

1)I have successfully ran the same nodes before encountering the error. 2)I might have accidentally upgraded something using apt-get upgrade.

3)I have already tried sourcing the following in my .bashrc

source /opt/ros/humble/setup.bash
source /home/pav/workspaces/udemy_ros2_ws/install/setup.bash

4)I have already tried deleting and re-creating the package and build, using the following:

ros2 pkg create udemy_ros2_pkg --build-type ament_cmake

followed by

colcon build

5) I have already reinstalled ROS2 using the following:

sudo apt remove ros-humble-desktop ros-dev-tools
sudo apt install ros-humble-desktop ros-dev-tools
sudo apt install python3-colcon* --only-upgrade

6) The complete error message is as follows:

  $ ros2 run demo_nodes_py listener
Traceback (most recent call last):
  File "/opt/ros/humble/lib/demo_nodes_py/listener", line 33, in <module>
    sys.exit(load_entry_point('demo-nodes-py==0.20.3', 'console_scripts', 'listener')())
  File "/opt/ros/humble/lib/demo_nodes_py/listener", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/ros/humble/lib/python3.10/site-packages/demo_nodes_py/topics/listener.py", line 21, in <module>
    from std_msgs.msg import String
ImportError: cannot import name 'String' from 'std_msgs.msg' (/opt/ros/humble/local/lib/python3.10/dist-packages/std_msgs/msg/__init__.py)
[ros2run]: Process exited with failure 1

7) The error seems to point to the following directory

/opt/ros/humble/local/lib/python3.10/dist-packages/std_msgs/msg

However, running

$ ls | grep -i string

returns

_string.py
_string_s.c

Does this mean that the expected String library is missing? If so, how can I fix it?

8) I'm using Ubuntu 22.04.1 LTS x86_64 and my ROS2 Env. Variables are as follows:

$ env | grep ROS
ROS_VERSION=2
ROS_PYTHON_VERSION=3
ROS_DOMAIN_ID=10
ROS_LOCALHOST_ONLY=0
ROS_DISTRO=humble
edit retag flag offensive close merge delete