ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It turns out we had already solved this problem in a different project. See https://github.com/tesseract-robotics/tesseract_python/tree/master/tesseract_python for an example.

The solution is to use colcon hooks to update PYTHONPATH.

First, add the following to CMakeLists.txt:

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/ament_index/resource_index/packages/${PROJECT_NAME} "") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/ament_index/resource_index/packages/${PROJECT_NAME} DESTINATION share/ament_index/resource_index/packages) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/ament_prefix_path.dsv "prepend-non-duplicate;AMENT_PREFIX_PATH;") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/ament_prefix_path.dsv DESTINATION share/${PROJECT_NAME}/hook)

if (${PYTHON_VERSION_MAJOR} LESS 3) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv "prepend-non-duplicate;PYTHONPATH;lib/python2.7/dist-packages") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv DESTINATION share/${PROJECT_NAME}/hook) else() file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv "prepend-non-duplicate;PYTHONPATH;lib/python3/dist-packages") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv DESTINATION share/${PROJECT_NAME}/hook) endif()

This assumes that PROJECT_NAME exactly matches the ROS package name. See https://github.com/tesseract-robotics/tesseract_python/blob/8d9db169c63c2b238c6fe8ea06042983cdc51a4a/tesseract_python/CMakeLists.txt#L155 for an example.

Then create a colcon.pkg file with the following contents, replacing <PROJECT_NAME> with the name of the package. It must match the CMakeLists PROJECT_NAME exactly.

{ "hooks": ["share/<project_name>/hook/ament_prefix_path.dsv", "share/<project_name>/hook/python_path.dsv"] }

Once the hook is created, the PYTHONPATH should be updated when the colcon setup scripts are sourced.

click to hide/show revision 2
No.2 Revision

It turns out we had already solved this problem in a different project. See https://github.com/tesseract-robotics/tesseract_python/tree/master/tesseract_python for an example.

The solution is to use colcon hooks to update PYTHONPATH.

First, add the following to CMakeLists.txt:

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/ament_index/resource_index/packages/${PROJECT_NAME} "") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/ament_index/resource_index/packages/${PROJECT_NAME} DESTINATION share/ament_index/resource_index/packages) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/ament_prefix_path.dsv "prepend-non-duplicate;AMENT_PREFIX_PATH;") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/ament_prefix_path.dsv DESTINATION share/${PROJECT_NAME}/hook)

share/${PROJECT_NAME}/hook)

if (${PYTHON_VERSION_MAJOR} LESS 3) file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv "prepend-non-duplicate;PYTHONPATH;lib/python2.7/dist-packages") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv DESTINATION share/${PROJECT_NAME}/hook) else() file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv "prepend-non-duplicate;PYTHONPATH;lib/python3/dist-packages") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/${PROJECT_NAME}/hook/python_path.dsv DESTINATION share/${PROJECT_NAME}/hook) endif()

This assumes that PROJECT_NAME exactly matches the ROS package name. See https://github.com/tesseract-robotics/tesseract_python/blob/8d9db169c63c2b238c6fe8ea06042983cdc51a4a/tesseract_python/CMakeLists.txt#L155 for an example.

Then create a colcon.pkg file with the following contents, replacing <PROJECT_NAME> with the name of the package. It must match the CMakeLists PROJECT_NAME exactly.

 {
    "hooks": ["share/<project_name>/hook/ament_prefix_path.dsv", "share/<project_name>/hook/python_path.dsv"]
["share/<PROJECT_NAME>/hook/ament_prefix_path.dsv", "share/<PROJECT_NAME>/hook/python_path.dsv"]
}

Once the hook is created, the PYTHONPATH should be updated when the colcon setup scripts are sourced.