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