ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

ImportError: No module named rospkg

asked 2020-07-01 06:56:25 -0500

Alessandro Melino gravatar image

updated 2020-07-08 04:04:28 -0500

Hello.

I am trying to execute an scrip for hw_interface, but in the roslaunch execution the following error emerges:

Traceback (most recent call last):
      File "/home/pi/catkin_ws/src/kate_global/script/hwinterface_script_kate.py", line 11, 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
    ImportError: No module named rospkg
    qt5ct: using qt5ct plugin
    [KATE_interface-4] process has died [pid 17094, exit code 1, cmd /home/pi/catkin_ws/src/kate_global/script/hwinterface_script_kate.py __name:=KATE_interface __log:=/home/pi/.ros/log/99d73b08-bb8f-11ea-9c44-dca6326fbe0e/KATE_interface-4.log].

If I try to install rospkg with

sudo apt-get install python3-rospkg

It sais that is alredy installed:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-rospkg is already the newest version (1.2.8-100).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

So I don't know why is this happening. Any idea?

I am working on Debian 10 (64 bits) and ROS version Noetic.

Best regards. Alessandro

EDIT:

The output of echo $PYTHONPATH is:

/home/pi/catkin_ws/devel/lib/python3/dist-packages:/opt/ros/noetic/lib/python3/dist-packages:/usr/local/lib

The first line of hwinterface_script_kate.py is:

#!/usr/bin/env python
edit retag flag offensive close merge delete

Comments

whats the output of:

echo $PYTHONPATH

and how did you try to execute your script?

lmiller gravatar image lmiller  ( 2020-07-01 09:50:00 -0500 )edit
1

I would guess that the script you invoke hwinterface_script_kate.py contains a shebang line referencing python or python2. Can you please update the question with the first line of that script.

Dirk Thomas gravatar image Dirk Thomas  ( 2020-07-02 09:14:22 -0500 )edit

I have alredy edited the question.

Alessandro Melino gravatar image Alessandro Melino  ( 2020-07-08 04:04:32 -0500 )edit

5 Answers

Sort by » oldest newest most voted
2

answered 2020-07-09 06:25:09 -0500

mebasoglu gravatar image

In this case, you should change the first line of your code with #!/usr/bin/env python3 like @Dirk Thomas said.

edit flag offensive delete link more

Comments

Doesn't work. The problem appears to be how ROS changes the Python environment relative to where it copies and runs python scripts.

Mr Moose gravatar image Mr Moose  ( 2021-01-16 12:26:48 -0500 )edit
1

answered 2022-05-05 04:36:42 -0500

Bernat Gaston gravatar image

None of the above solutions worked for me. I changed several times the python version and nothing. The only thing that worked for me was

 sudo pip install --target=/opt/ros/noetic/lib/python3/dist-packages rospkg
edit flag offensive delete link more

Comments

It worked for me. Thanks a lot.

ArmanAsq gravatar image ArmanAsq  ( 2022-07-28 12:37:22 -0500 )edit
0

answered 2020-08-13 01:42:23 -0500

You need a file named __init__.py (two underscores on each side) in every folder in the hierarchy. This is what python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions but even if you create them empty this will solve it. To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

edit flag offensive delete link more

Comments

Doesn't work. The problem appears to be how ROS changes the Python environment relative to where it copies and runs python scripts.

Mr Moose gravatar image Mr Moose  ( 2021-01-16 12:27:05 -0500 )edit
0

answered 2021-03-02 19:27:21 -0500

russv gravatar image

A bit of a late response, but I had this issue also and did not see the any relevant responses to what worked for me. The following steps resolved my problem:

1) Ensure that your CMakeList.txt is updated. Update the section shown blow in code with your python filename:

catkin_install_python(PROGRAMS script/hwinterface_script_kate.py script/"additional_python_scripts.py"
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Where "script/" is the directory that contains the python file, and the "additional_python_scripts.py" text emphasizes to add additional python scripts that may also be used.

2) Make the python file executable. You do this in the terminal by going to your file directory and using "chmod". Do this for all your python scripts.

cd ~/catkin_ws/src/kate_global/script/  
chmod +x hwinterface_script_kate.py

3) I ran "catkin_make" again, i don't know if this is necessary but it worked for me.

cd ~/catkin_ws
catkin_make
edit flag offensive delete link more
-1

answered 2020-07-01 19:15:46 -0500

mebasoglu gravatar image

Hello, Noetic uses Python3 and you may be using Python2 to execute your code.

I'm using Ubuntu 20.04 and it doesn't have Python2 pre-installed. In my case, creating a python soft link which points to python3 in /usr/bin/ solved the problem.

sudo ln -sf python3 python

But in Debian 10 python command might be running Python2, I don't know. Check it with:

ls /usr/bin/python*

edit flag offensive delete link more

Comments

Hello.

The output of ls /usr/bin/python* is: python, python2, python2.7, python3 and python3.7. Should I do sudo ln -sf python3 python then?

Alessandro Melino gravatar image Alessandro Melino  ( 2020-07-08 04:00:23 -0500 )edit

Okay. Thank you for your help. That worked fine.

Alessandro Melino gravatar image Alessandro Melino  ( 2020-07-09 06:30:21 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2020-07-01 06:56:25 -0500

Seen: 7,706 times

Last updated: May 05 '22