Hi,
I am having difficulty importing files from another package.
inorder to use import scripts from another package, the following syntax was used during package creation:
catkin_create_pkg big_pkg rospy small_pkg
here big_pkg is the new package and small_pkg is the package containing the files to be imported. I have used this syntax in the past and was able to import the files without any problem but now i get the error:
Traceback (most recent call last):
File “/home/joseph/catkin_ws/src/big_pkg/src/big_pkg_script.py”, line 3, in
from small_pkg.src import small_pkg_script
ModuleNotFoundError: No module named ‘small_pkg’
Both big_pkg and small_pkg are located at ~/catkin_ws/src.
Structure of big_pkg
Structure of small_pkg
Contents of big_pkg_script.py
#! /usr/bin/env python3
from small_pkg.src import small_pkg_script
print("Inside Big_Pkg_script")
When i run the file big_pkg_script.py, i get the following error, even though i had added small_pkg as a dependency of big_pkg during package creation:
Traceback (most recent call last):
File "/home/joseph/catkin_ws/src/big_pkg/src/big_pkg_script.py", line 3, in <module>
from small_pkg.src import small_pkg_script
ModuleNotFoundError: No module named 'small_pkg'
i have not made any changes into the package.xml files since adding a package as dependency duirng package creation takes care of the content of package.xml files as well.
Adding a package as a dependency of another package in ROS1 does not make all its source code available. It’s for sharing custom messages in the catkin_ws/devel folder.
If you want to share source code among your packages:
run this command (add to your ~/.bashrc for a permanent effect):
In every folder of your packages, create an empty __init__.py file to indicate that it’s a module.
You might even have some default imports to this file so you can easily import them. See this example, taken from the ROS Basics exam package:
# /home/user/catkin_ws/devel/lib/python3/dist-packages/basics_exam/msg/__init__.py
from ._RecordPoseAction import *
from ._RecordPoseActionFeedback import *
from ._RecordPoseActionGoal import *
from ._RecordPoseActionResult import *
from ._RecordPoseFeedback import *
from ._RecordPoseGoal import *
from ._RecordPoseResult import *
from ._record_odomAction import *
from ._record_odomActionFeedback import *
from ._record_odomActionGoal import *
from ._record_odomActionResult import *
from ._record_odomFeedback import *
from ._record_odomGoal import *
from ._record_odomResult import *
Then you can run
from any_package_under_catkin_ws_src.src_or_anysubfolders.script import anyClassOrObject
PS: The above might not work for a package that has a custom message, because it would already have a module in catkin_ws/devel/lib/python3/dist-packages/.