小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
程序运行报错:module 'rospkg' has no attribute 'RosPack'
-
在conda的base环境下运行小车控制程序
python run.py
时发生报错
AttributeError: module 'rospkg' has no attribute 'RosPack'
重新分别使用pip和pip3安装rospkg,都显示
Requirement already satisfied: rospkg in /home/xxx/anaconda3/lib/pyhon3.8/site-packages(1.40)
请教下这是什么问题呢?是因为rospkg1.4版本不稳定吗?
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
dir(rospkg)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec']通过这一行可以看出来你的rospkg是空的,有两个办法。
- 看rospkg源码,看看为何为空
- 尝试降低rospkg版本或同时降低python版本
建议先尝试方案2,比较快
使用第二个方法,降低rospkg版本
1、卸载现版本rospkgpip uninstall rospkg
2、安装低版本rospkg
pip install rospkg==1.2.9
至于装哪个版本,我参考的是这里,显示非testing状态的版本
https://tracker.debian.org/pkg/ros-rospkg/news/?page=1
3、测试
(base) xxx@xxx-virtual-machine:~/catkin_ws/src/scripts$ python Python 3.8.8 (default, Apr 13 2021, 19:58:26) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> rospkg.RosPack() <rospkg.rospack.RosPack object at 0x7f1a11689d60>
问题解决,谢谢小鱼的极速且耐心的回复!这对初学者真的非常有帮助,非常感谢!
-
@1687028512 你用的ROS版本是多少?rospkg版本是否为最新版。
-
@小鱼 ROS版本为melodic,rospkg使用的是最新版
-
@1687028512 运行这个指令的上下文是否有有ROS。可以运行下面指令看一下
python3 -c "import sys; print('\n'.join(sys.path))" python2 -c "import sys; print('\n'.join(sys.path))"
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
上下文是否有ROS
运行了这两个命令,得到的结果如下
xxx@xxx-virtual-machine:~$ python3 -c "import sys; print('\n'.join(sys.path))" /opt/ros/melodic/lib/python2.7/dist-packages /usr/lib/python36.zip /usr/lib/python3.6 /usr/lib/python3.6/lib-dynload /usr/local/lib/python3.6/dist-packages /usr/lib/python3/dist-packages xxx@xxx-virtual-machine:~$ python2 -c "import sys; print('\n'.join(sys.path))" /opt/ros/melodic/lib/python2.7/dist-packages /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /usr/lib/python2.7/lib-tk /usr/lib/python2.7/lib-old /usr/lib/python2.7/lib-dynload /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages/wx-3.0-gtk3
从这两条结果的第一行看起来是有ROS的样子
-
@1687028512 虽然有ROS,但是没有发现conda的环境,比较奇怪。再试试Python的
python -c "import sys; print('\n'.join(sys.path))"
-
另,可能我给的报错信息还不够完善,在此补充一下
(base)xxx@xxx-virtual-machine:~/catkin_ws/src/scripts$ python run.py Traceback (most recent call last): File "run.py", line 6, 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 48, in <module> _rospack = rospkg.RosPack() AttributeError: module 'rospkg' has no attribute 'RosPack'
-
@1687028512 试一下手动import看看是否有对应的包
Python 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> dir(rospkg) ['InvalidManifest', 'MANIFEST_FILE', 'Manifest', 'ResourceNotFound', 'RosPack', 'RosStack', 'STACK_FILE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'common', 'environment', 'expand_to_packages', 'get_etc_ros_dir', 'get_log_dir', 'get_package_name', 'get_ros_home', 'get_ros_package_path', 'get_ros_paths', 'get_ros_root', 'get_stack_version_by_dir', 'get_test_results_dir', 'list_by_path', 'manifest', 'on_ros_path', 'parse_manifest_file', 'rospack', 'stack'] >>> rospkg.RosPack() <rospkg.rospack.RosPack object at 0x7f8c369d0b20>
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 虽然有ROS,但是没有发现conda的环境,比较奇怪。再试试Python的
python -c "import sys; print('\n'.join(sys.path))"
运行结果如下
(base) xxx@xxx-virtual-machine:~/catkin_ws/src/scripts$ python -c "import sys; print('\n'.join(sys.path))" /opt/ros/melodic/lib/python2.7/dist-packages /home/xxx/anaconda3/lib/python38.zip /home/xxx/anaconda3/lib/python3.8 /home/xxx/anaconda3/lib/python3.8/lib-dynload /home/xxx/anaconda3/lib/python3.8/site-packages /home/xxx/anaconda3/lib/python3.8/site-packages/locket-0.2.1-py3.8.egg
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 试一下手动import看看是否有对应的包
Python 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> dir(rospkg) ['InvalidManifest', 'MANIFEST_FILE', 'Manifest', 'ResourceNotFound', 'RosPack', 'RosStack', 'STACK_FILE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'common', 'environment', 'expand_to_packages', 'get_etc_ros_dir', 'get_log_dir', 'get_package_name', 'get_ros_home', 'get_ros_package_path', 'get_ros_paths', 'get_ros_root', 'get_stack_version_by_dir', 'get_test_results_dir', 'list_by_path', 'manifest', 'on_ros_path', 'parse_manifest_file', 'rospack', 'stack'] >>> rospkg.RosPack() <rospkg.rospack.RosPack object at 0x7f8c369d0b20>
收到,试试这个
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 试一下手动import看看是否有对应的包
Python 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> dir(rospkg) ['InvalidManifest', 'MANIFEST_FILE', 'Manifest', 'ResourceNotFound', 'RosPack', 'RosStack', 'STACK_FILE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'common', 'environment', 'expand_to_packages', 'get_etc_ros_dir', 'get_log_dir', 'get_package_name', 'get_ros_home', 'get_ros_package_path', 'get_ros_paths', 'get_ros_root', 'get_stack_version_by_dir', 'get_test_results_dir', 'list_by_path', 'manifest', 'on_ros_path', 'parse_manifest_file', 'rospack', 'stack'] >>> rospkg.RosPack() <rospkg.rospack.RosPack object at 0x7f8c369d0b20>
运行结果如下
(base) xxx@xxx-virtual-machine:~/catkin_ws/src/scripts$ python Python 3.8.8 (default, Apr 13 2021, 19:58:26) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> dir(rospkg) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__'] >>> ropkg.RosPack() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'ropkg' is not defined
-
@1687028512 rospkg,不是ropkg,少了个s
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 rospkg,不是ropkg,少了个s
修改后如下
>>> rospkg.RosPack() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'rospkg' has no attribute 'RosPack'
-
@1687028512 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
dir(rospkg)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec']通过这一行可以看出来你的rospkg是空的,有两个办法。
- 看rospkg源码,看看为何为空
- 尝试降低rospkg版本或同时降低python版本
建议先尝试方案2,比较快
-
@小鱼 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
@1687028512 在 程序运行报错:module 'rospkg' has no attribute 'RosPack' 中说:
dir(rospkg)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec']通过这一行可以看出来你的rospkg是空的,有两个办法。
- 看rospkg源码,看看为何为空
- 尝试降低rospkg版本或同时降低python版本
建议先尝试方案2,比较快
使用第二个方法,降低rospkg版本
1、卸载现版本rospkgpip uninstall rospkg
2、安装低版本rospkg
pip install rospkg==1.2.9
至于装哪个版本,我参考的是这里,显示非testing状态的版本
https://tracker.debian.org/pkg/ros-rospkg/news/?page=1
3、测试
(base) xxx@xxx-virtual-machine:~/catkin_ws/src/scripts$ python Python 3.8.8 (default, Apr 13 2021, 19:58:26) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import rospkg >>> rospkg.RosPack() <rospkg.rospack.RosPack object at 0x7f1a11689d60>
问题解决,谢谢小鱼的极速且耐心的回复!这对初学者真的非常有帮助,非常感谢!
-
@1687028512 客气啦,没事常来社区看看,互相帮助,共同提高~
-
-
@小鱼 一定一定~