Solve ImportError: No module named rospkg by specifying a python interpreter

Keywords: Python Ubuntu


When I first wrote a blog, there were too many solutions to similar problems on CSDN, but they couldn't solve my problem. This time, it was not easy to solve my problem. I hope to record this solution and help students with similar problems. Maybe it's written incorrectly, maybe it's written so that people don't understand it. In fact, the process of exploration is not useful, you can jump to it directly Solution

Problem Description

down from github ethz-asl/panoptic_mapping This project, in execution of roslaunch panoptic_ Mapping_ Error ImportError in ROS run.launch command: No module named rospkg

Environment ubutnu 20.04, has successfully installed ros noetic, the current environment is Python 3.8.5, Python 3.7 and python 3.9 have been installed, as well as ubuntu's own Python 2.7, has been installed anaconda, but there is no activation of conda environment, no conflict. Here is the complete error information:

Traceback (most recent call last):
  File "/home/leequer/catkin_ws/src/panoptic_mapping/panoptic_mapping_utils/src/flat_dataset/flat_data_player.py", line 7, 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

Exploration process

Many on the Web have said that import rospkg failed because it conflicts with anaconda, commenting on the part of Anaconda in the ~/.bashrc file, and then re-source.

But I don't have this conflict here, nor do I install it in Python 2, nor is it useful to annotate anaconda in the bashrc file. The pip install rospkg hint is already installed in / usr/lib/python3/dist-packages, and import ing these two packages directly into the python environment at the terminal will succeed:

leequer@leequer-PC:~/catkin_ws/devel$ python
Python 3.8.5 (default, May 27 2021, 13:30:53) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rospy
>>> import rospkg
>>> rospy.__file__
'/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py'
>>> rospkg.__file__
'/usr/lib/python3/dist-packages/rospkg/__init__.py'
>>> 

But in the case of roslaunch panoptic_ Mapping_ An error occurred when ROS run.launch.

Considering the error, can rospy and ROS lib be installed in / opt/ros/noetic/lib/python3/dist-packages/installed in the same directory? So try to copy the rospkg package directly from the previous directory to the later directory. If the rospkg error disappears, but the new error is that the dependent package of rospkg is missing, then use the following command to specify the installation directory and install rospkg:

sudo pip install --target=/opt/ros/noetic/lib/python3/dist-packages rospkg

Because of previous replication, rospkg already exists, overwrite the original package with -upgrade

sudo pip install --target=/opt/ros/noetic/lib/python3/dist-packages rospkg --upgrade

There were still errors:

ERROR: launchpadlib 1.10.13 requires testresources, which is not installed.

After installing testresources, execute roslaunch panoptic_ Mapping_ The ROS run.launch error changed:

Traceback (most recent call last):
  File "/home/leequer/catkin_ws/src/panoptic_mapping/panoptic_mapping_utils/src/flat_dataset/flat_data_player.py", line 7, in <module>
    import rospy
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py", line 47, in <module>
    from std_msgs.msg import Header
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/__init__.py", line 1, in <module>
    from ._Bool import *
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module>
    import genpy
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/__init__.py", line 34, in <module>
    from . message import Message, SerializationError, DeserializationError, MessageException, struct_I
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 48, in <module>
    import yaml
  File "/opt/ros/noetic/lib/python3/dist-packages/yaml/__init__.py", line 362
    class YAMLObject(metaclass=YAMLObjectMetaclass):
                              ^
SyntaxError: invalid syntax

Answer in stackoverflow Failed to launch rospy file: SyntaxError: invalid syntax YAMLObject(metaclass=YAMLObjectMetaclass): The following answers were found in the

I found the solution!
Turns out all I had to do was to change #!/usr/bin/env python import rospy To: #!/usr/bin/env python3 import rospy

Go search#!/ usr/bin/env, find the blog #!/ usr/bin/python3 and #!/ Role of usr/bin/env python3 And finally understand the cause of the error and how to solve it.

Solution

The Python interpreter is specified at the beginning of the original Python file, and #!/ The purpose of usr/bin/env python is to find out which one to calculate in your environment, which is of course prone to error! Find the python file in the error message, and I'm flat_data_player.py, open this file,

At the beginning of the python file with an error, set #!/ usr/bin/env python to #!/ Usr/bin/python 3.8, specify the python interpreter, and finally no more errors, problem solving.

summary

#!/ Usr/bin/env/python3 means to find the location of the python3 interpreter from the PATH Environment Variable. Instead of writing the path to death, find the installation path of the python3 interpreter in the Environment Variable and call the interpreter under that path to execute the script.
Obviously, use #!/ The usr/bin/env python3 is more flexible and versatile and is recommended
Author: patiencing
Links: https://www.jianshu.com/p/400c612381dd
Source: Short Book
Copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

Although this is a flexible way to write, errors often occur for newbies of ours who are not familiar with the ubuntu environment configuration. The common error is the python environment conflict between anaconda and ros. The errors I encounter this time may not be very common, but they are always a problem with the Python interpreter selection. By specifying an interpreter at the beginning of the python file, you can avoid having the installation package downloaded and prompting you that the installation package could not be found.

The two main reference blogs are provided as hyperlinks in this article, which is no longer a single column

Posted by Major Tom on Mon, 29 Nov 2021 13:50:18 -0800