I am new to use ROS in my project. I have installed ROS2 humble on Ubuntu 22.04. I need to import different python codes/functions as modules to a ROS2 subscriber (python code).

They are:

1) `common_utils.py` in `utils` folder
2) `build_network` which is a function in `pointpillar.py`

Both python codes are located in another folder which is not a ROS2 package (home/user/Machine_Learning) and its directory is different from subscriber directory (home/user/DL_ws/src/object_detection/object_detection/pc_subscriber.py).

In my subscriber code, I changed the directory to Machine_Learning folder, then I used from utils import common_utils and from pointpillar import build_network. With/without updating package.xml (adding utils and pointpillar for </exec_depend>), then colcon build, when I use ros2 run object_detection listener, I get this error: ModuleNotFoundError: No module named 'utils' or ModuleNotFoundError: No module named 'pointpillar'.

How can I solve this issue?

I appreciate any help in advance.




1 Answer 1


You can't add miscellaneous directories of python code to your ros project using package.xml. In the short term, you can manually add the full directory path to the PYTHONPATH environment variable (this is a native python feature, not a ros feature.)

However, creating this type of dependency is fragile and not a good idea if you expect to ever share this code with anyone else. It would be better to create an actual ros package so you can document the dependency in the object_detection package.xml.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.