It turns out we had already solved this problem in a different project. See https://github.com/tesseract-robotics... 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... 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.
Hi @johnw Just an observation the question’s title doesn’t connect well with the tags and actual question. This may limit the number of people that review the question
@osilva apparently the title got borked, I've fixed it.
that makes so much sense @johnw. Thank you for updating.