Can't import rospy because of ModuleNotFoundError: No module named 'rospkg'

987 Views Asked by At

I started working with ROS and I am having some troubles with the environment and imports (i know that's dumb)... Basically I can't import the rospy library in my python file, which should be straightforward.

#!/usr/bin/env python3

import rospy
from rosplan_knowledge_msgs.msg import KnowledgeItem
from rosplan_knowledge_msgs.srv import KnowledgeUpdateService, KnowledgeUpdateServiceRequest

class PlatformStatusHandler : 
    def __init__(self, robot_id, energy_level, health_status, current_profile) -> None:
        self.robot_id = robot_id
        self.energy_level = energy_level
        self.health_status = health_status 

        self.current_profile = current_profile

    
    def test_rosplan_db(self, ins_type, ins_name, update_type) : 
            
            rospy.wait_for_service('/rosplan_knowledge_base/update')
            knowledge_update_proxy = rospy.ServiceProxy('/rosplan_knowledge_base/update', KnowledgeUpdateService)

            req = KnowledgeUpdateServiceRequest()
            req.knowledge.knowledge_type = KnowledgeItem.INSTANCE
            req.knowledge.instance_type = ins_type
            req.knowledge.instance_name = ins_name
            req.update_type = update_type
            success = success and knowledge_update_proxy(req).success
(new_env) benjies@benjies-Precision-3571:~/Workspace_c/SoaR_pipeline$ python3 PlatformStatusHandler.py 
Traceback (most recent call last):
  File "PlatformStatusHandler.py", line 3, in <module>
    import rospy
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/client.py", line 52, in <module>
    import roslib
  File "/opt/ros/noetic/lib/python3/dist-packages/roslib/__init__.py", line 50, in <module>
    from roslib.launcher import load_manifest  # noqa: F401
  File "/opt/ros/noetic/lib/python3/dist-packages/roslib/launcher.py", line 42, in <module>
    import rospkg
ModuleNotFoundError: No module named 'rospkg'

That is the code I am working on but I can't go beyond the first line. Here is the content of my ROS_PACKAGE_PATH if that's of any help, maybe I am messing things up on that level.

echo $ROS_PACKAGE_PATH
/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_action_interface:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_dispatch_msgs:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_knowledge_msgs:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_dependencies:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_knowledge_base:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_planning_system:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_rqt:/home/benjies/ROSPlan_workspace/src/ROSPlan/rosplan_sensing_interface:/home/benjies/catkin_ws/src:/opt/ros/noetic/share
1

There are 1 best solutions below

0
S. McCarthy On

It's possible you haven't sourced your ROS install directory in the terminal window you're using. ROS doesn't do this automatically, to avoid problems if people have multiple distros installed - try something along the lines of:

# Source our ROS2 installation
source /opt/ros/humble/setup.bash
source ~/dev/ros2/install/setup.bash

Either each time you want to run ros code in a terminal, or you could add these lines to your .bashrc file if you don't mind them always being sourced