ROS Noetic环境下无法导入自定义Python类的问题求助
问题:ROS Noetic中Python自定义类导入失败
环境信息
- Python 3.8.10
- Ubuntu 20.04
- ROS Noetic
错误日志
Traceback (most recent call last): File "/home/belal/git/decision_making_pacbot/devel/lib/ros_planning/ros_plan.py", line 15, in <module> exec(compile(fh.read(), python_script, 'exec'), context) File "/home/belal/git/decision_making_pacbot/src/ros_planning/src/ros_planning/ros_plan.py", line 8, in <module> from problem_handler import ProblemHandler ImportError: cannot import name 'ProblemHandler' from 'problem_handler' (/home/belal/git/decision_making_pacbot/devel/.private/ros_planning/lib/ros_planning/problem_handler.py)
文件层级结构
src ├── ros_planning │ ├── CMakeLists.txt | ├── package.xml | ├── setup.py │ └── src │ └── ros_planning | ├── ros_plan.py | ├── problem_handler.py | └── __init__.py
配置文件内容
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2) project(ros_planning) find_package(catkin REQUIRED COMPONENTS rospy) catkin_package(CATKIN_DEPENDS) catkin_install_python(PROGRAMS src/${PROJECT_NAME}/problem_handler.py src/${PROJECT_NAME}/ros_plan.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) catkin_python_setup() install(DIRECTORY model downward launch DESTINATION "${CATKIN_PACKAGE_SHARE_DESTINATION}")
package.xml
<?xml version="1.0"?> <package format="2"> <name>ros_planning</name> <version>0.0.0</version> <description>The ros_planning package</description> <maintainer email="s@todo.todo">s</maintainer> <license>MIT</license> <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> <build_export_depend>roscpp</build_export_depend> <build_export_depend>rospy</build_export_depend> <build_export_depend>std_msgs</build_export_depend> <exec_depend>roscpp</exec_depend> <exec_depend>rospy</exec_depend> <exec_depend>std_msgs</exec_depend> </package>
setup.py
#!/usr/bin/python3 from distutils.core import setup from catkin_pkg.python_setup import generate_distutils_setup setup_args = generate_distutils_setup( scripts=['src/ros_planning/problem_handler.py', 'src/ros_planning/ros_plan.py'], packages=['ros_planning'], package_dir={'': 'src'}, requires=['rospy', 'std_msgs'] ) setup(**setup_args)
相关代码片段
problem_handler.py中的类定义:
class ProblemHandler: def __init__(self, path): self.path = path
ros_plan.py中的导入语句:
from problem_handler import ProblemHandler
解决方案
1. 修正导入语句
ROS Noetic基于Python3,相对导入规则更严格,需替换原有导入方式:
- 方式一:使用完整包路径导入
from ros_planning.problem_handler import ProblemHandler - 方式二:使用包内相对导入
from .problem_handler import ProblemHandler
2. 调整CMakeLists.txt执行顺序
catkin_python_setup()必须在catkin_install_python()前执行,同时移除库模块的PROGRAMS配置:
修改后的CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.2) project(ros_planning) find_package(catkin REQUIRED COMPONENTS rospy) catkin_package(CATKIN_DEPENDS) # 优先执行Python包配置 catkin_python_setup() # 仅安装可执行脚本,库模块由catkin_python_setup自动处理 catkin_install_python(PROGRAMS src/${PROJECT_NAME}/ros_plan.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) install(DIRECTORY model downward launch DESTINATION "${CATKIN_PACKAGE_SHARE_DESTINATION}")
3. 清理编译空间并重新编译
catkin clean --all catkin_make source devel/setup.bash
原因说明
- Python3对相对导入的兼容性要求比Python2(ROS Melodic使用版本)更严格,旧导入方式不再适用。
- CMake配置顺序错误,导致库模块被当作可执行脚本安装,破坏了包内模块的正常引用路径。
内容的提问来源于stack exchange,提问作者Bilal
相关产品推荐
相关产品推荐

