ModuleNotFoundError: No module named 'ament_package'

asked 2019-06-09 09:29:19 -0600

petal gravatar image

updated 2019-06-09 10:17:11 -0600

gvdhoorn gravatar image

Hello! I am using ROS2 dashing for building my program. I also have ROS Melodic in my system. For ROS2 instead of sourcing, the environment from source /opt/ros/dashing/setup.bash I am setting the CMAKE_PREFIX_PATH in CMakeLists.txt.

SET(CMAKE_PREFIX_PATH "${CMAKE_REFIX_PATH} home/pankhuri/ros2_ws/install;/opt/ros/dashing")

While building with colcon build --symlink-install My code works well but now since I have to avoid sourcing the path I am getting this error:

pankhuri@pankhuri-LENOVO-IDEAPAD-500-15ISK:~/week2$ colcon buildStarting >>> dummyexample
--- stderr: dummyexample                         
Traceback (most recent call last):
  File "/opt/ros/dashing/share/ament_cmake_core/cmake/package_templates/", line 21, in <module>
    from ament_package.templates import get_environment_hook_template_path
ModuleNotFoundError: No module named 'ament_package'
CMake Error at /opt/ros/dashing/share/ament_cmake_core/cmake/ament_cmake_package_templates-extras.cmake:41 (message):
  returned error code 1
Call Stack (most recent call first):
  /opt/ros/dashing/share/ament_cmake_core/cmake/ament_cmake_coreConfig.cmake:38 (include)
  /opt/ros/dashing/share/ament_cmake/cmake/ament_cmake_export_dependencies-extras.cmake:15 (find_package)
  /opt/ros/dashing/share/ament_cmake/cmake/ament_cmakeConfig.cmake:38 (include)
  CMakeLists.txt:13 (find_package)

Failed   <<< dummyexample       [ Exited with code 1 ]

Summary: 0 packages finished [0.36s]
  1 package failed: dummyexample
  1 package had stderr output: dummyexample

Some Snippets of my CMakeLists.txt are:

cmake_minimum_required(VERSION 3.5)

 #Default to C++14


  add_compile_options(-Wall -Wextra -Wpedantic -Wunused-parameter)

SET(CMAKE_PREFIX_PATH "${CMAKE_REFIX_PATH} home/pankhuri/ros2_ws/install;/opt/ros/dashing")
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)


add_executable(main main.cpp)
target_link_libraries(main ${rclcppLIBRARIES} ${std_msgs_LIBRARIES} )

install(TARGETS main
        DESTINATION bin)

Any suggestion in this matter would be helpful..

edit retag flag offensive close merge delete


since I have to avoid sourcing the path

can you clarify why?

gvdhoorn gravatar image gvdhoorn  ( 2019-06-09 10:00:38 -0600 )edit

ament_package is a Python module. Python uses the PYTHONPATH variable. It doesn't retrieve the CMAKE_PREFIX_PATH, so setting is will not extend the Python search path.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-09 10:01:40 -0600 )edit

I am working on extending the support of some tools to ROS2 in such a way that they are compatible with both ROS1 and ROS2. So, I have to get both ROS1 and ROS2 libraries in CMakeLists.txt for compiling conditionally.

Setting CMAKE_PREFIX_PATH for ROS1 worked well without the need of sourcing the ROS Melodic. I wanted to do something similar for ROS2 too. I Understood why setting the CMAKE_PREFIX_PATH won't work for it. Can you suggest any alternative for this?

petal gravatar image petal  ( 2019-06-09 10:27:00 -0600 )edit

Setting CMAKE_PREFIX_PATH for ROS1 worked well without the need of sourcing the ROS Melodic

As long as none of your ROS nodes are rospy based or roscpp based and need additional libraries, this could work. The environment will not be correctly setup though (missing ROS_MASTER_URI, ROS_HOME, LD_LIBRARY_PATH, ROS_DISTRO, ROS_VERSION, ROS_PACKAGE_PATH and a nr of other variables).

gvdhoorn gravatar image gvdhoorn  ( 2019-06-09 11:15:54 -0600 )edit

My ROS does have cpp file (rclcpp), which gives error here..

petal gravatar image petal  ( 2019-06-09 14:45:14 -0600 )edit

The error is in a Python script, which can't import a certain Python module. The problem is in ament_cmake_core, not in your node.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-09 14:48:33 -0600 )edit

I set the pythonpath first and then CMake_PREFIX_PATH with:

SET(ENV{PYTHONPATH} "/opt/ros/dashing/lib/python3.6/site-packages/")

SET(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH} home/pankhuri/ros2_ws/install;/opt/ros/dashing")

And this problem got solved.

petal gravatar image petal  ( 2019-06-10 01:11:33 -0600 )edit

Unless really necessary, I would recommend to not manually mutate these sort of environment variables.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-10 01:50:07 -0600 )edit