当前位置: 首页 > article >正文

在环境冲突情况下调整优先级以解决ROS Catkin构建中缺少模块的问题【ubuntu20.04】

在机器人操作系统(ROS)的开发过程中,构建工作空间时遇到各种依赖性问题是常见的挑战之一。尤其是在多Python环境共存的情况下,环境变量的冲突往往导致诸如缺少empy模块等错误。本文将详细介绍在ROS Noetic与Anaconda Python环境共存的情况下,如何通过调整环境变量优先级来解决Catkin构建失败的问题。

一、引言

随着机器人技术的发展,ROS(Robot Operating System)已成为机器人软件开发的标准框架。然而,开发过程中涉及多种工具和依赖库,尤其是Python环境的配置,常常成为潜在的障碍。特别是在系统Python与Anaconda等第三方Python环境共存时,环境变量的优先级可能导致ROS无法正确找到所需的模块,如empy,从而导致Catkin构建失败。
本文将以一个实际案例为基础,深入探讨在环境冲突情况下如何调整环境变量优先级,确保ROS的正常构建与运行。通过系统化的步骤和详尽的解释,帮助开发者快速定位并解决类似问题,提升开发效率

二、问题描述

在尝试使用Catkin构建ROS工作空间时,系统报出如下错误信息:

sunshine@sunshine:~/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws$ catkin_make
Base path: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws
Source space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/src
Build space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build
Devel space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel
Install space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/install
####
#### Running command: "cmake /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/src -DCATKIN_DEVEL_PREFIX=/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel -DCMAKE_INSTALL_PREFIX=/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/install -G Unix Makefiles" in "/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/noetic
-- This workspace overlays: /opt/ros/noetic
-- Using PYTHON_EXECUTABLE: /home/sunshine/SoftWare/Anaconda/bin/python3
-- Using Debian Python package layout
-- Could NOT find PY_em (missing: PY_EM) 
CMake Error at /opt/ros/noetic/share/catkin/cmake/empy.cmake:30 (message):
  Unable to find either executable 'empy' or Python module 'em'...  try
  installing the package 'python3-empy'
Call Stack (most recent call first):
  /opt/ros/noetic/share/catkin/cmake/all.cmake:164 (include)
  /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:20 (include)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build/CMakeFiles/CMakeOutput.log".
Invoking "cmake" failed

经过检查,系统中并未找到empy模块,且即便通过apt-get安装了python3-empy,问题依旧存在。进一步排查发现,当前使用的是Anaconda的Python环境,而ROS Noetic期望使用系统自带的Python环境。这导致python3-empy安装在系统Python下,而Anaconda的Python环境无法识别该模块,进而引发构建失败

三 解决方案

1、确认当前Python环境

首先,确认当前系统使用的Python环境以及empy模块的安装情况

which python3
python3 -c "import em"

如果which python3输出的是Anaconda的路径(如/home/sunshine/SoftWare/Anaconda/bin/python3),且import em报错ModuleNotFoundError: No module named ‘em’,则说明当前Python环境无法识别empy模块

2、安装empy模块

由于ROS Noetic与系统Python环境高度集成,推荐使用系统包管理器安装empy

sudo apt-get update
sudo apt-get install python3-empy

安装完成后,验证安装是否成功:

which empy
python3 -c "import em"

理想情况下,which empy应返回/usr/bin/empy,且import em不应再报错

三 优先使用系统的Python环境

为了确保ROS使用系统的Python环境,可以通过临时调整PATH环境变量,使系统的Python和empy优先于Anaconda的环境。
在当前终端会话中,运行以下命令:

export PATH=/usr/bin:$PATH

解释:该命令将系统的/usr/bin目录添加到PATH的最前面,确保系统的Python和empy被优先调用。

验证调整是否生效

which python3
# 应输出 /usr/bin/python3

which empy
# 应输出 /usr/bin/empy

python3 -c "import em"
# 无错误输出

四、重新构建Catkin工作空间

(base) sunshine@sunshine:~/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws$ export PATH=/usr/bin:$PATH
(base) sunshine@sunshine:~/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws$ catkin_make
Base path: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws
Source space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/src
Build space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build
Devel space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel
Install space: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/install
####
#### Running command: "cmake /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/src -DCATKIN_DEVEL_PREFIX=/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel -DCMAKE_INSTALL_PREFIX=/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/install -G Unix Makefiles" in "/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build"
####
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/noetic
-- This workspace overlays: /opt/ros/noetic
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Using Debian Python package layout
-- Found PY_em: /usr/lib/python3/dist-packages/em.py  
-- Using empy: /usr/lib/python3/dist-packages/em.py
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build/test_results
-- Forcing gtest/gmock from source, though one was otherwise available.
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") 
-- Found Threads: TRUE  
-- Using Python nosetests: /usr/bin/nosetests3
-- catkin 0.8.10
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build
####
#### Running command: "make -j24 -l24" in "/home/sunshine/Instrument/WitHighModbus_HWT9073485/ROS/wit/wit_ros_ws/build"
####

构建完成!!

四 长期解决方案

如果频繁需要在Anaconda和系统Python环境之间切换,建议采用以下长期解决方案

1、创建独立的终端会话

在需要构建ROS工作空间时,开启一个不激活Anaconda环境的新终端,确保使用系统的Python环境

2、 使用虚拟环境

为ROS创建一个专用的Python虚拟环境,隔离其依赖:

python3 -m venv ros_env
source ros_env/bin/activate
pip install empy

在此环境中构建ROS工作空间,避免与其他Python项目的依赖冲突。

3、修改Anaconda的激活脚本

在激活Anaconda环境后,手动将系统的/usr/bin添加回PATH的优先位置

conda activate your_env_name
export PATH=/usr/bin:$PATH

这样可以确保在使用Anaconda环境的同时,系统的Python和empy依然优先可用。

五、总结

在多Python环境共存的系统中,确保ROS使用系统的Python环境是避免依赖性问题的关键。通过临时调整PATH环境变量,可以快速解决缺少empy模块导致的Catkin构建失败问题。然而,为了长期稳定地开发ROS项目,建议采用独立的终端会话或虚拟环境,确保ROS与其他Python项目的依赖隔离。这不仅提升了开发效率,也减少了潜在的兼容性问题。


http://www.kler.cn/a/470424.html

相关文章:

  • HTTP/HTTPS ②-Cookie || Session || HTTP报头
  • 2024.1.5总结
  • 基于Informer网络实现电力负荷时序预测——cross validation交叉验证与Hyperopt超参数调优
  • ESP32-C3 AT WiFi AP 启 TCP Server 被动接收模式 + BLE 共存
  • 【three.js】场景搭建
  • Nginx——静态资源部署(二/五)
  • Outlook 网页版一直提示:检测到重复的重定向
  • Selenium 的四种等待方式及使用场景
  • C# 检查一个字符串是否是科学计数法格式字符串 如 1.229266E+01
  • Requests聚焦爬虫-数据解析
  • 项目48:简易语言学习助手【源代码】 --- 《跟着小王学Python·新手》
  • 线程的创建与管理:Java的多重身份
  • 【React】刷新页面或跳转路由时进行二次确认
  • 【问题记录】SpringBoot 解决 getReader() has already been called for this request 错误
  • F#语言的计算机基础
  • HTML - <link>
  • 03、MySQL安全管理和特性解析(DBA运维专用)
  • Python:类方法、实例方法与静态方法深度解析(补)
  • (安卓无线调试)ADB 无法连接及 Scrcpy 问题排查指南
  • 机器学习算法---贝叶斯学习
  • 城市安全风险综合监测预警平台
  • 阿里云 人工智能与机器学习
  • 动漫推荐系统django+vue前台后台完整源码
  • 这是什么操作?强制迁移?GitLab 停止中国区用户访问
  • 专业学习|BFS算法介绍以及实现框架
  • RK3588平台开发系列讲解(系统篇)Linux Kconfig的语法