ModuleNotFoundError: No module named 'rospkg'
Asked Answered
F

3

5

I am new to ROS,

I have a problem when I import rospy into my script file example.py and run it: It says:

Traceback (most recent call last):
  File "/home/nagarjunv/hk_ws/src/rvo/src/example.py", line 4, in <module>
    import rospy
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/__init__.py", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/client.py", line 52, in <module>
    import roslib
  File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/__init__.py", line 50, in <module>
    from roslib.launcher import load_manifest  # noqa: F401
  File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/launcher.py", line 42, in <module>
    import rospkg
ModuleNotFoundError: No module named 'rospkg'

I am importing the following into my script file:The line 4 is rospy

#!/usr/bin/env python3

import sys
import rospy

THis is my CmakeLists:

cmake_minimum_required(VERSION 3.0.2)
project(rvo)

find_package(catkin REQUIRED COMPONENTS
  rospy
  std_msgs
  message_generation
)

 generate_messages(
   DEPENDENCIES
   std_msgs
 )

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES rvo
  CATKIN_DEPENDS rospy std_msgs
#  DEPENDS system_lib
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

This is my package xml:

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_export_depend>message_generation</build_export_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>message_runtime</exec_depend>

I specify both in CMakelists and Package XML 'rospy' and importing it. I also checked my ros has rospy package available. BUt, I dont understand why I am having trouble running my script file?

rosrun rvo example.py 

I will be glad to provide more details about this error. Can any please let me know the solution to this problem?

Feudalism answered 9/3, 2021 at 20:49 Comment(2)
If my answer solved you problem, please mark it as such. Otherwise do provide more info.Sanitize
@Nagarjun Vinukonda How did you solve issue?Incept
V
7

Try to change the shebang (line 1) according to the python version your ROS distro works with.

For melodic or earlier: #!/usr/bin/env python2

For noetic or later: #!/usr/bin/env python3

Vociferant answered 1/2, 2022 at 21:1 Comment(0)
S
2

Depending on your ROS version and respectively the one of rospkg you may need to stick to python 2.x or 3.x.

Your script is calling python3 interpreter

#!/usr/bin/env python3

but you can clearly see in the error log that a different version is used by your installation:

File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/init.py", line 49, in from .client import spin, myargv, init_node, \

If you want to use Python 3 for a ROS installation that is based on Python 2.7 you need to build everything from scratch but even then the chances for success are pretty slim.

Here are some possible solution:

  • If you want to use Python 3, see here. According to this article ROS Noetic is targeting only Python 3.
  • If you don't want to upgrade, you need to stick to Python 2.7.
  • If the version of Python is of no concern, just adapt your script to use the one your ROS installation is depending on.
Sanitize answered 9/3, 2021 at 20:54 Comment(0)
S
0

Maybe a python problem, but I was using ros on ubuntu 20.04 terminal and couldnt use rosrun rospy_tutorials talker because of this error. When I tried to download using python, it showed already installed. The ony thing worked for me is:

 sudo pip install --target=/opt/ros/noetic/lib/python3/dist-packages rospkg
Swampy answered 26/6 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.