Call your own python module from the ROS package

I don't know how to import a collection of functions that are not related to my own ROS in a ROS package, so until now I made a dedicated ROS package and imported it. After all, I wanted to make it independent, so I tried a method like that with trial and error.

File structure

catkin_ws
├─ build
├─ devel
└─ src
   ├─ my_py_utils
   │  ├── __init__.py(Empty file)
   │  └── util_functions.py
   └─ my_ros_pkg
      ├── CMakeLists.txt
      ├── __init__.py(Empty file)
      ├── package.xml
      └── src
          ├── __init__.py
          └── robot_controller.py

It's almost like this. Here, consider using the function (do_something) in util_functions.py from robot_controller.py.

What I tried

Pattern to do properly for the time being

in robot_controller.py

from my_py_utils.util_functions import do_something 

Is inserted. ↓

ImportError: No module named my_py_utils.util_functions

become.

Pattern to try with relative path

in robot_controller.py

from ...my_py_utils.util_functions import do_something 

Is inserted. ↓ pycharm recognizes it, but when it comes to rosrun

ValueError: Attempted relative import in non-package

become.

A pattern that will teach you the path properly

in robot_controller.py

import sys
sys.path.append('/home/[UserName]/catkin_ws/src')
from my_py_utils.util_functions import do_something 

Is inserted. ↓ It works properly!

Finally

After all, I've only tried the method of importing as normal python, but it's troublesome to specify the absolute path, and I have a theory that I should use ROS package.xml, so I'd like to try it again. (I want someone to tell me) Also, I didn't understand much about python or ROS than I expected (´ ・ ω ・ `). Let's do our best.

Recommended Posts

Call your own python module from the ROS package
[Python] logging in your own module
[Python] Package and distribute your own modules
Call your own C language shared library from Python using ctypes
Make your own module quickly with setuptools (python)
Use the nghttp2 Python module from Homebrew from pyenv's Python
Call Polly from the AWS SDK for Python
Flow of creating your own package with setup.py with python
Let's call your own C ++ library with Python (Preferences)
Use the CASA Toolkit in your own Python environment
Call CPLEX from Python (DO cplex)
About the Python module venv
Call the API with python3.
[Ansible] How to call variables when creating your own module
Get your heart rate from the fitbit API in Python!
Run the intellisense of your own python library with VScode.
(Note) How to pass the path of your own module
Play music from USB speakers using the ROS package (gx_sound_player)
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
[Python] When you want to import and use your own package in the upper directory
Call Matlab from Python to optimize
Call a Python function from p5.js.
[Python] Make your own LINE bot
Call popcount from Ruby / Python / C #
Operate Maya from an external Python interpreter using the rpyc module
Use the Flickr API from Python
Call python from nim with Nimpy
Bridge ROS to your own protocol
Try using the Python Cmd module
Call C / C ++ from Python on Mac
Master the weakref module in Python
Call c language from python (python.h)
Create your own Linux commands in Python
Create wordcloud from your tweet with python3
[LLDB] Create your own command in Python
Pass the path of the imported python module
Easily use your own functions in Python
Learning notes from the beginning of Python 1
Control the Matrix LED panel from ROS
[Python] Register your own library on PyPI
Until you install your own Python library
To import your own module with jupyter
[Python] Import the module one level higher
Check the path of the Python imported module
Call the python debugger at any time
Launch the Python interpreter from Git bash
Create a deb file from a python package
Publish your own Python library with Homebrew
From Python 3.4, pip becomes the standard installer! ??
Homebrew search from python module name (ShellScript)
Call a command from Python (Windows version)
Learning notes from the beginning of Python 2
Install the Python module in any directory
[Python] Get the main color from the screenshot
Get your own IP address in Python
The road to updating Splunkbase with your own Splunk app for Python v2 / v3
[Python] Implement your own list-like class using collections.UserList
[Python] Let's execute the module regularly using schedule
ROS Package for getting RGB-D Images from RGB-D Sensor
Install the python package in an offline environment
Use Python in your environment from Win Automation