No module named bb8_move_circle_class

Respected Prof,
As I’m running the roslaunch my_python_class bb8_move_circle_service_server.launch code in part 7 of ROS_Basice, I’m getting the following error:

This is not exercise and the codes are already in the course. But I’ll put the codes down, in case you want to see:

Python File: bb8_move_circle_class.py
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist

class MoveBB8():

def __init__(self):
    self.bb8_vel_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
    self.cmd = Twist()
    self.ctrl_c = False
    self.rate = rospy.Rate(10) # 10hz
    rospy.on_shutdown(self.shutdownhook)
    
def publish_once_in_cmd_vel(self):
    """
    This is because publishing in topics sometimes fails the first time you publish.
    In continuous publishing systems, this is no big deal, but in systems that publish only
    once, it IS very important.
    """
    while not self.ctrl_c:
        connections = self.bb8_vel_publisher.get_num_connections()
        if connections > 0:
            self.bb8_vel_publisher.publish(self.cmd)
            rospy.loginfo("Cmd Published")
            break
        else:
            self.rate.sleep()
    
def shutdownhook(self):
    # works better than the rospy.is_shutdown()
    self.ctrl_c = True

def move_bb8(self, linear_speed=0.2, angular_speed=0.2):
    
    self.cmd.linear.x = linear_speed
    self.cmd.angular.z = angular_speed
    
    rospy.loginfo("Moving BB8!")
    self.publish_once_in_cmd_vel()

if name == ‘main’:
rospy.init_node(‘move_bb8_test’, anonymous=True)
movebb8_object = MoveBB8()
try:
movebb8_object.move_bb8()
except rospy.ROSInterruptException:
pass

Python File: bb8_move_circle_service_server.py
#! /usr/bin/env python

import rospy
from std_srvs.srv import Empty, EmptyResponse 
from bb8_move_circle_class import MoveBB8

def my_callback(request):
    rospy.loginfo("The Service move_bb8_in_circle has been called")
    movebb8_object = MoveBB8()
    movebb8_object.move_bb8()
    rospy.loginfo("Finished service move_bb8_in_circle")
    return EmptyResponse() 

rospy.init_node('service_move_bb8_in_circle_server') 
my_service = rospy.Service('/move_bb8_in_circle', Empty , my_callback)
rospy.loginfo("Service /move_bb8_in_circle Ready")
rospy.spin() # mantain the service open.

Note: When I want to put launch file codes here with tags, they aren’t visible after posting it. So I’ve to remove tags from the codes to make them visible.
Launch File: bb8_move_circle_service_server.launch
launch
!-- Start Service Server for move_bb8_in_circle service –
node pkg=“my_python_class” type=“bb8_move_circle_service_server.py” name=“service_move_bb8_in_circle_server” output=“screen”
/node
/launch

I’ve given permissions to python files. I’ve also tried removing built files and running catkin_make and source devel/setup.bash again. It didn’t work.
Do I need to change something in CMakeLists.txt and package.xml files ? If yes, would you please tell me what do I need to include there?
If no, would you please tell me solution to this error ?

Hello @abdulbasitisdost,

If you want to do it as you have the code right now, you should have a look at this and see how to properly import modules. Another alternative, easier at this point and assuming that both of your Python files are in the same directory, would be to directly import the Python file with:

import bb8_move_circle_class

This will import the whole Python script into the bb8_move_circle_service_server.py file.

Best,

Prof,
I didn’t understand this sentence below:

And I wrote import bb8_move_circle_class instead of from bb8_move_circle_class import MoveBB8 … It didn’t work either. It’s throwing the same error as above which says No module named bb8_move_circle_class

Hi Abdul,

Did you check that both python files are in the same directory?

Yeah… worked after putting both the files in the same directory
Thank you Professor Rodrigo @roalgoal
I wonder why does it not work when the files were in different directory ?

1 Like

That’s how importing files works, and it’s good practice. Python looks in the same directory by default.

Rodrigo

1 Like