Skip to content

Instantly share code, notes, and snippets.

@zkytony
Last active October 10, 2023 20:46
Show Gist options
  • Save zkytony/fb7616b745d77025f6abe919c41f8ea4 to your computer and use it in GitHub Desktop.
Save zkytony/fb7616b745d77025f6abe919c41f8ea4 to your computer and use it in GitHub Desktop.
Import python modules between ROS packages

Reference: Installing Python scripts and modules

Package structure

├── my_pkg
│   ├── setup.py
│   ├── scripts
│   │   └── executable_script
    └── src
        └── my_pkg
            ├── __init__.py
            └── my_module.py
  • There needs to be a setup.py.
  • Executable scripts should go under my_pkg/scripts or my_pkg/nodes.
  • Importable modules should be placed under src/my_pkg.
  • There needs to be an __init__.py under src/my_pkg.

setup.py

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['my_pkg'],
    package_dir={'': 'src'},
    requires=['std_msgs', 'rospy', 'rosbag', 'tf']
)

setup(**setup_args)

Never run python setup.py install. Only use catkin to install the module.

Compile

Run

catkin_make

Then

source catkin_ws/devel/setup.bash

Now you should be able to import the code in my_pkg from any other location on your computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment