Robotics StackExchange | Archived questions

import file in sub-folder when building ros2 package using python

Dear developer, thank you for your time.

We are wondering, how to add the sub-folder in the main folder when using python for ros2-foxy package.

I found that, after colcon build, all files in the main folder will be moved to install/map/lib/python3.6/site-packages/XXX, but the sub-folders under XXX won't. So, what is the right way to include subfolders?

Thanks a lot!

Asked by lilyGinger on 2022-06-05 06:24:25 UTC

Comments

Answers

Don't you mean to install them like in this part of the tutorial there?

Install other files in a ROS2 Python package

You can virtually put everything you want in a ROS2 package. There is no hard rule about what to do, but some conventions make it easier for you. Let’s see how to install launch files and YAML config files. Those are among the most common things you’ll add to packages when you develop your ROS2 application. Launch files

Create a launch/ folder at the root of your package. You’ll put all your launch files inside this folder.

cd ~/ros2_ws/src/my_python_pkg/

mkdir launch

Now, to install those launch files, you need to modify setup.py.

import os
from glob import glob
from setuptools import setup
...
data_files=[
    ('share/ament_index/resource_index/packages',
        ['resource/' + package_name]),
    ('share/' + package_name, ['package.xml']),
    (os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
],
...

For our example, with package name my_python_pkg, this will install all launch files from the launch/ folder, into ~/ros2_ws/install/my_python_pkg/share/my_python_pkg/launch/.

Note: you only need to modify setup.py once. After that, every time you add a launch file you’ll just need to compile your package so that the file is installed, that’s it.

Then, to start a launch file:

ros2 launch package_name launch_file_name

YAML config files

You can follow the same technique to install YAML config files.

Create a config/ folder at the root of your package. You’ll put all your YAML files here. $ cd ~/ros2_ws/src/my_python_pkg/ $ mkdir config

To install YAML files, again, modify setup.py. Add a new line in the data_files array:

  ...
  data_files=[
         ('share/ament_index/resource_index/packages',
             ['resource/' + package_name]),
         ('share/' + package_name, ['package.xml']),
         (os.path.join('share', package_name, 'launch'),
  glob('launch/*.launch.py')), (os.path.join('share', package_name,  'config'),
  glob('config/*.yaml')),
  ],
  ...

Still with the my_python_pkg example, the YAML files will be installed into ~/ros2_ws/install/my_python_pkg/share/my_python_pkg/config/.

Asked by ljaniec on 2022-06-05 12:29:26 UTC

Comments