本人unbuntu20.04安装了ROS noetic和anaconda环境之后,在使用过程在遇到了很多不方便的地方.该文章提供了一种便捷的解决方案.
问题
1.在虚拟环境中运行ros程序出现了ModuleNotFoundError: No module named 'rospkg'
的错误,根据网上教程在虚拟环境中安装以下模块
pip install rospkg rospy catkin_tools
2.catkin_make报错,原因是编译时使用了虚拟环境中的python
Traceback (most recent call last):
File "/opt/ros/noetic/share/gencpp/cmake/../../../lib/gencpp/gen_cpp.py", line 49, in <module>
genmsg.template_tools.generate_from_command_line_options(
File "/opt/ros/noetic/lib/python3/dist-packages/genmsg/template_tools.py", line 213, in generate_from_command_line_options
generate_from_file(argv[1], options.package, options.outdir, options.emdir, options.includepath, msg_template_dict, srv_template_dict)
File "/opt/ros/noetic/lib/python3/dist-packages/genmsg/template_tools.py", line 154, in generate_from_file
_generate_msg_from_file(input_file, output_dir, template_dir, search_path, package_name, msg_template_dict)
File "/opt/ros/noetic/lib/python3/dist-packages/genmsg/template_tools.py", line 93, in _generate_msg_from_file
_generate_from_spec(input_file,
File "/opt/ros/noetic/lib/python3/dist-packages/genmsg/template_tools.py", line 77, in _generate_from_spec
interpreter = em.Interpreter(output=ofile, globals=g, options={em.RAW_OPT:True,em.BUFFERED_OPT:True})
AttributeError: module 'em' has no attribute 'RAW_OPT'
make[2]: *** [shibie/CMakeFiles/shibie_generate_messages_cpp.dir/build.make:63:/home/cch/detect/devel/include/shibie/detect_msg.h] 错误 1
make[2]: *** 正在删除文件“/home/cch/detect/devel/include/shibie/detect_msg.h”
make[1]: *** [CMakeFiles/Makefile2:513:shibie/CMakeFiles/shibie_generate_messages_cpp.dir/all] 错误 2
make: *** [Makefile:141:all] 错误 2
解决办法为退出虚拟环境再编译,
conda deactivate
catkin_make
也可以指定python编译
catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
解决方案
本文提供了一种解决ROS与anaconda环境冲突问题比较简单的方法
首先在.bashrc文件中最后一行添加
conda deactivate
其次在你运行的python文件中第一行添加你的虚拟环境python路径,比如我的路径为
#! /home/c/anaconda3/envs/YOLOv7/bin/python3
这样每次打开终端不需要进入虚拟环境也可以直接运行你的python程序.