ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

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

asked 2022-06-05 06:24:25 -0600

lilyGinger gravatar image

updated 2022-06-05 12:24:37 -0600

ljaniec gravatar image

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!

edit retag flag offensive close merge delete

Comments

omers gravatar image omers  ( 2022-09-18 04:13:35 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2022-06-05 12:29:26 -0600

ljaniec gravatar image

updated 2022-06-05 12:32:09 -0600

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/.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-06-05 06:24:25 -0600

Seen: 445 times

Last updated: Jun 05 '22