Ask Your Question
1

ROS2 Python relative import of my scritps

asked 2020-04-18 01:24:05 -0600

robopo gravatar image

updated 2020-04-20 12:17:07 -0600

Hi guys, I have a project on Raspberry pi 3, a compiled ROS2 foxy from source, install Kivy and everything if fine. But now I'm trying to port my UI from ROS1 to ROS2 and I can't figure out how to do relative imports like from utils. See to code.

Package(ROS2) hierarchy:

control_panel_ui
├── control_panel_ui
│   ├── graphical_interface.py
│   ├── __init__.py
│   ├── listeners
│   │   ├── __init__.py
│   │   └── start_listeners.py
│   ├── media
│   │   └── image.png
│   └── utils
│       ├── dict_services.py
│       ├── get_rpc.py
│       ├── __init__.py
│       ├── states.py
│       └── vehicle_state.py
├── package.xml
├── setup.cfg
├── setup.py
├── ...

And the code. Kivy is imported fine but my code such as utils, listeners etc. doesn't.

#kivy.require('1.7.2')
from kivy.app import App
 ...
from kivy.clock import Clock

from .utils.vehicle_state import VehicleState
from .utils.dict_services import dict_serv
from .utils.get_rpc import get_rpc_function

...

After the colcon build [package].py is moved to the install directory but those utils doesn't and then it it says cannot find package.

edited: Maybe the thing is I don't know how to configure setup.py/.cfg. Maybe a link or an example would be great. I was not able to find anything uselful.

added setup.py/.cfg

from setuptools import setup

package_name = 'control_panel_ui'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],    
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml'])
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='pi',
    maintainer_email='michal@michal.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'control_panel_ui = control_panel_ui.graphical_interface:graphical_interface'
        ],
    },
)

cfg

[develop]
script-dir=$base/lib/control_panel_ui
[install]
install-scripts=$base/lib/control_panel_ui
edit retag flag offensive close merge delete

Comments

You need to share the content of your setup.py / setup.cfg files.

Dirk Thomas gravatar image Dirk Thomas  ( 2020-04-20 11:03:13 -0600 )edit

1 Answer

Sort by » oldest newest most voted
3

answered 2020-04-20 12:33:06 -0600

Dirk Thomas gravatar image

The subdirectories are not available because in the setup.py file you only specify the first level directory:

packages=[package_name]

Instead of enumerating them explicitly I suggest you use the function provided by setuptools to find them:

from setuptools import find_packages
packages=find_packages(exclude=['test'])
edit flag offensive delete link more

Comments

Thank you so much! This answer is truly a lifesaver!

LSD gravatar image LSD  ( 2021-05-27 14:19:37 -0600 )edit

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: 2020-04-18 01:24:05 -0600

Seen: 591 times

Last updated: Apr 20 '20