执行colcon build时出现catkin_pkg模块缺失错误求助
解决ROS2 Colcon构建时
ModuleNotFoundError: No module named 'catkin_pkg'错误 问题详情
作为Ubuntu和ROS2新手,编写完URDF模型代码后,执行colcon build --symlink-install时触发以下错误:
Starting >>> my_package --- stderr: my_package Traceback (most recent call last): File "/opt/ros/jazzy/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 22, in <module> from catkin_pkg.package import parse_package_string ModuleNotFoundError: No module named 'catkin_pkg' CMake Error at /opt/ros/jazzy/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:95 (message): execute_process(/usr/local/bin/python3.13 /opt/ros/jazzy/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py /home/arya/learn_ros2_ws/src/my_package/package.xml /home/arya/learn_ros2_ws/build/my_package/ament_cmake_core/package.cmake) returned error code 1 Call Stack (most recent call first): /opt/ros/jazzy/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml) /opt/ros/jazzy/share/ament_lint_auto/cmake/ament_lint_auto_find_test_dependencies.cmake:31 (ament_package_xml) CMakeLists.txt:32 (ament_lint_auto_find_test_dependencies) --- Failed <<< my_package [0.23s, exited with code 1] Summary: 0 packages finished [0.36s] 1 package failed: my_package 1 package had stderr output: my_package
已尝试pip install catkin_pkg但问题未解决,附上URDF代码:
<?xml version="1.0"?> <robot name="my_first_robot" xmlns:xacro="http://ros.org/wiki/xacro"> <xacro:property name="base_width" value = "0.31"/> <xacro:property name="base_length" value = "0.42"/> <xacro:property name="base_height" value = "0.18"/> <xacro:property name="wheel_radius" value="0.10"/> <xacro:property name="wheel_width" value="0.04"/> <xacro:property name="wheel_ygap" value="0.025"/> <xacro:property name="wheel_zoff" value="0.01"/> <xacro:property name="wheel_xoff" value="0.05"/> <xacro:property name="caster_xoff" value="0.1"/> <!-- Define intertial property macros --> <xacro:macro name="box_inertia" params="m w h d"> <inertial> <origin xyz="0 0 0" rpy="${pi/2} 0 ${pi/2}"/> <mass value="${m}"/> <inertia ixx="${(m/12) * (h*h + d*d)}" ixy="0.0" ixz="0.0" iyy="${(m/12) * (w*w + d*d)}" iyz="0.0" izz="${(m/12) * (w*w + h*h)}"/> </inertial> </xacro:macro> <xacro:macro name="cylinder_inertia" params="m r h"> <inertial> <origin xyz="0 0 0" rpy="${pi/2} 0 0" /> <mass value="${m}"/> <inertia ixx="${(m/12) * (3*r*r + h*h)}" ixy = "0" ixz = "0" iyy="${(m/12) * (3*r*r + h*h)}" iyz = "0" izz="${(m/2) * (r*r)}"/> </inertial> </xacro:macro> <xacro:macro name="sphere_inertia" params="m r"> <inertial> <mass value="${m}"/> <inertia ixx="${(2/5) * m * (r*r)}" ixy="0.0" ixz="0.0" iyy="${(2/5) * m * (r*r)}" iyz="0.0" izz="${(2/5) * m * (r*r)}"/> </inertial> </xacro:macro> <!-- Robot Base --> <link name="base_link"> <visual> <origin xyz="0 0 ${base_height/2}" rpy="0 0 0"/> <geometry> <box size="${base_length} ${base_width} ${base_height}"/> </geometry> <material name="Cyan"> <color rgba="0 1.0 1.0 1.0"/> </material> </visual> <collision> <origin xyz="0 0 ${base_height/2}" rpy="0 0 0"/> <geometry> <box size="${base_length} ${base_width} ${base_height}"/> </geometry> </collision> <xacro:box_inertia m="5" w="${base_width}" d="${base_length}" h="${base_height}"/> </link> <gazebo reference="base_link"> <material>Gazebo/Orange</material> </gazebo> <!-- Caster Wheel --> <link name="caster"> <visual> <geometry> <sphere radius="${wheel_zoff}"/> </geometry> <material name="white"> <color rgba="1 1.0 1.0 1.0"/> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0"/> <geometry> <sphere radius="${wheel_zoff}"/> </geometry> </collision> <xacro:sphere_inertia m="0.5" r="${(wheel_radius+wheel_zoff-(base_height/2))}"/> </link> <gazebo reference="caster"> <material>Gazebo/Blue</material> </gazebo> <joint name="caster_joint" type="fixed"> <parent link="base_link"/> <child link="caster"/> <origin xyz="${-caster_xoff} 0.0 0" rpy="0 0 0"/> </joint> <!-- Wheels --> <xacro:macro name="wheel" params="prefix x_reflect y_reflect"> <link name="${prefix}_link"> <visual> <origin xyz="0 0 0" rpy="${pi/2} 0 0"/> <geometry> <cylinder radius="${wheel_radius}" length="${wheel_width}"/> </geometry> <material name="Gray"> <color rgba="0.5 0.5 0.5 1.0"/> </material> </visual> <collision> <origin xyz="0 0 0" rpy="${pi/2} 0 0"/> <geometry> <cylinder radius="${wheel_radius}" length="${wheel_width}"/> </geometry> </collision> <xacro:cylinder_inertia m="0.5" r="${wheel_radius}" h="${wheel_width}"/> </link> <joint name="${prefix}_joint" type="continuous"> <parent link="base_link"/> <child link="${prefix}_link"/> <origin xyz="${-x_reflect*wheel_xoff} ${y_reflect*(base_width/2+wheel_ygap)} ${wheel_radius-wheel_zoff}" rpy="0 0 0"/> <axis xyz="0 1 0"/> </joint> </xacro:macro> <xacro:wheel prefix="drivewhl_l" x_reflect="-1" y_reflect="1" /> <xacro:wheel prefix="drivewhl_r" x_reflect="-1" y_reflect="-1" /> <gazebo> <plugin name='diff_drive' filename='libgazebo_ros_diff_drive.so'> <!-- wheels --> <left_joint>drivewhl_l_joint</left_joint> <right_joint>drivewhl_r_joint</right_joint> <!-- kinematics --> <wheel_separation>0.4</wheel_separation> <wheel_diameter>0.2</wheel_diameter> <!-- limits --> <max_wheel_torque>80</max_wheel_torque> <max_wheel_acceleration>1.0</max_wheel_acceleration> <!-- output --> <publish_odom>true</publish_odom> <publish_odom_tf>false</publish_odom_tf> <publish_wheel_tf>true</publish_wheel_tf> <odometry_frame>odom</odometry_frame> <robot_base_frame>base_link</robot_base_frame> </plugin> </gazebo> </robot>
解决步骤
1. 优先使用系统包管理器安装catkin_pkg
ROS2工具链依赖系统级Python包,pip安装可能因环境隔离无法被colcon识别,执行以下命令:
sudo apt update && sudo apt install python3-catkin-pkg
2. 修复Python环境路径
从错误日志可见colcon使用的是/usr/local/bin/python3.13,并非ROS2默认依赖的Python版本。临时调整PATH优先级,让系统Python先被调用:
export PATH=/usr/bin:$PATH
之后重新执行构建命令:
colcon build --symlink-install
3. 确保ROS2环境正确激活
每次打开新终端或构建前,必须source ROS2的setup文件:
source /opt/ros/jazzy/setup.bash
若工作空间已有构建产物,还需source本地安装目录:
source ~/learn_ros2_ws/install/setup.bash
4. 验证包配置文件(可选)
检查package.xml是否包含必要依赖:
<depend>xacro</depend> <depend>gazebo_ros</depend>
同时确保CMakeLists.txt正确查找依赖:
find_package(ament_cmake REQUIRED) find_package(xacro REQUIRED) find_package(gazebo_ros REQUIRED) ament_package()
内容的提问来源于stack exchange,提问作者dips
相关产品推荐
相关产品推荐

