Error importing packages from another package?

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
Screenshot from 2022-06-18 11-07-30

Structure of small_pkg
Screenshot from 2022-06-18 11-10-51



Contents of big_pkg_script.py

#! /usr/bin/env python3
from small_pkg.src import small_pkg_script
print("Inside Big_Pkg_script")

Content of small_pkg_script.py

#! /usr/bin/env python3
def small_func():
    print("Small func called")
print("Inside small_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'


Can you tell me what went wrong?

Could it be that you have renamed the package folder, but not updated package.xml, @Joseph1001

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.

Try compiling your workspace, then source ~/catkin_ws/devel/setup.bash, @Joseph1001

just tried that, still the same

I have replicated the same in a rosject, can you check it?

and run the following command?
rosrun big_pkg big_pkg_script.py

Please note that i am getting the same error in the ROSjet as well. i would really like to know where i went wrong @bayodesegun @ralves @albertoezquerro @duckfrost2

We are not checking right now because we are hosting the ROS Developers Day right now.

We will come back to you when when possible, @Joseph1001

oh ok, no problem, whenever you are free. And all the best with ROS Developers Day, a lot of interesting Speakers.

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:

  1. run this command (add to your ~/.bashrc for a permanent effect):
export PYTHONPATH=$PYTHONPATH:/home/user/catkin_ws/src
  1. 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/.

1 Like

Thanks you @bayodesegun . This worked. I was stuck on this for a while.

I request that this content aslo be added to ROS basics since this is essential to working with ROS packages.