How to fix ROS2 Package '...' not found: package '...' not found, searching ...

Problem

You want to load a package from a local directory instead of a global ROS2 search directory.

Say, you want to load my_package:

$ ls
my_package

and you use

AMENT_PREFIX_PATH=$(pwd) ros2 launch my_package my.launch.py

with the prefix path set to the current directory, expecting ROS2 to find ./my_package in the current directory.

However, you see an error such as

Package 'my_package' not found: "package 'my_package' not found, searching: ['/home/user/']"

Solution

When you set AMENT_PREFIX_PATH to, for example, /home/user, ROS2 will not search for ./my_package in /home/user.

Instead, it will look in /home/user/share/ament_index/resource_index/packages for a (potentially empty) file named my_package.

In other words, ROS2 expects the index to be in a specific directory structure:

/home/user/
└── share
    └── ament_index
        └── resource_index
            └── packages
                └── my_package

The official way to use the ROS2 package

You must build my_package and use the auto-generated setup scripts to set the correct environment variables! ROS2 does not officially support hacking around with a custom AMENT_PREFIX_PATH like this.

cd my_package
colcon build

After that, load the package using the package name:

source install/local_setup.sh

This will set

COLCON_PREFIX_PATH=/home/user/my_package/install

which contains the correct ament index directory structure.

Workaround

In case you don’t want to build the package, you can create a symlink to the package in the expected directory:

mkdir -p share/ament_index/resource_index/packages
touch share/ament_index/resource_index/packages/my_package

This will make ROS2 find the package. However, the correct share directory will still not be present, hence this is not recommended. In the future, I might add info on how to work around this.