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

Calling a local python script from a python launch file

asked 2021-08-05 10:21:57 -0500

ohf gravatar image

updated 2021-08-05 10:22:22 -0500

Using Foxy on Ubuntu 20.04:

I have a bringup package with multiple launch files and I am trying to put some common functionality (a few methods) in a utils.py python script.

launch directory: launch_file_1.py launch_file_2.py utils.py __init__.py # added because some answers to similar questions on the net suggested it could help. It didn't...

I haven't find any way to import my utils.py methods in the launch files but Python3 makes it very difficult to import relative modules and added a lot of rules.

I tried multiple syntaxes:

from utils import method1
# fails with: ModuleNotFoundError: No module named 'utils'
# Probably fails because this is not an installed module

from .utils import method1
# fails with: ImportError: attempted relative import with no known parent package

from .utils.py import method1
# fails with: ImportError: attempted relative import with no known parent package

It would be bizarre not to allow for such basic functionality. As a workaround, I am copying all my helper methods to every single launch file...

edit retag flag offensive close merge delete

Comments

Have the same exact question. Someone please respond.

spoluri gravatar image spoluri  ( 2021-10-20 17:12:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-10 16:41:59 -0500

tnajjar gravatar image

updated 2022-01-10 16:43:02 -0500

Try adding this to your launch file:

import os
import sys
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path)
from utils import method1
edit flag offensive delete link more

Comments

Thanks, I had exactly the same issue and this resolved it!!

leander gravatar image leander  ( 2022-07-07 04:24:24 -0500 )edit

Question Tools

4 followers

Stats

Asked: 2021-08-05 10:21:57 -0500

Seen: 329 times

Last updated: Jan 10 '22