Cannot import class from another script: exec(compile(fh.read(), python_script, 'exec'), context)

Dear community,

could you help me figure out why there is an import error? It seems that it can found but cannot be imported.

ser:~/catkin_ws$ rosrun wall_following find_wall_service_server.py
Traceback (most recent call last):
  File "/home/user/catkin_ws/devel/lib/wall_following/find_wall_service_server.py", line 15, in <module>
    exec(compile(fh.read(), python_script, 'exec'), context)
  File "/home/user/catkin_ws/src/wall_following/scripts/find_wall_service_server.py", line 5, in <module>
    from find_wall_movement import FindWallMove
ImportError: cannot import name 'FindWallMove' from 'find_wall_movement' (/home/user/catkin_ws/devel/lib/wall_following/find_wall_movement.py)

Here is the package structure:

Here is my script find_wall_service_server.py:

#!/usr/bin/env python

import rospy
from wall_following.srv import FindWall, FindWallResponse
from find_wall_movement import FindWallMove

class FindWallServiceServer:
    def __init__(self):
        self.service_name = "/find_wall"
        rospy.init_node('find_wall_service_server')
        
        self.find_wall = FindWallMove()
        self.service = rospy.Service(self.service_name, FindWall, self.callback)
        
        rospy.on_shutdown(self.find_wall.stop_robot)
        
        rospy.loginfo(f"Service {self.service_name} is ready")

    def callback(self, request):
        rospy.loginfo(f"Service {self.service_name} has been called")
        
        wall_found = self.find_wall.find_wall()

        rospy.loginfo(f"Finished ervice {self.service_name}.")
        
        return FindWallResponse(wallfound=wall_found)

    def spin(self):
        rospy.spin()

if __name__ == '__main__':
    service_server = FindWallServiceServer()
    try:
        service_server.spin()
    except rospy.ROSInterruptException:
        pass  # Normal shutdown, no additional action needed because on_shutdown will handle stop_robot
    except Exception as e:
        rospy.logerr(f"An unexpected error occurred: {e}")
        service_server.find_wall.stop_robot()

Here is another script find_wall_movement.py:

#!/usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

class FindWallMove:
    def __init__(self):
        self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.sub = rospy.Subscriber('/scan', LaserScan, self.laser_callback)

        self.rate = rospy.Rate(10)
        self.move_cmd = Twist()  

        self.laser_data = []
        self.front_index = None
        self.angle_increment = None


    def laser_callback(self, msg):
        self.laser_data = msg.ranges
        if self.front_index is None:
            self.calculate_laser_indices(msg)

    def calculate_laser_indices(self, msg):
         # Determine the indices for the key directions
        self.angle_increment = msg.angle_increment

        # Calculate indices 
        self.num_ray =len(msg.ranges)
        self.front_index = int((0 - msg.angle_min) / msg.angle_increment)
        self.right_index = int(self.front_index / 2)
        self.left_index = self.front_index + int((self.num_ray - self.front_index) / 2)

    def find_wall(self):
        rospy.loginfo("Finding the nearest wall...")

        # Step 1: Identify the shortest laser ray . Assume the closest obstacle is the wall (only in the simulation/real enviroment in this course).
        while not self.laser_data:
            self.rate.sleep()

        min_distance = min(self.laser_data)
        min_index = self.laser_data.index(min_distance)

        # Step 2: Rotate the robot until the front of it is facing the wall (front laser ray is the smallest one). 
        angle_to_rotate = (self.front_index - min_index) * self.angle_increment
        
        rospy.loginfo(f"Rotating by {angle_to_rotate} radians to face the nearest wall")
        self.rotate(angle_to_rotate)

        # Step 3: Move forward until the front ray is smaller than 30 cm
        while self.laser_data[self.front_index] > 0.3:
            self.move_cmd.angular.z = 0
            self.move_cmd.linear.x = 0.2
            self.pub.publish(self.move_cmd)
            self.rate.sleep()

        # Step 4: Rotate left by 90 degrees to align with the wall
        rospy.loginfo("Rotating left by 90 degrees to align with the wall")
        self.rotate(1.5708)  # Rotate 90 degrees left (π/2 radians)


        # Stop the robot after positioning
        self.stop_robot()
        return True

    def rotate(self, angle):
        # Rotate by a specific angle
        angular_speed = 0.2  # radians per second
        duration = abs(angle) / angular_speed
        self.move_cmd.linear.x = 0
        self.move_cmd.angular.z = (angular_speed if angle > 0 else -angular_speed)

        end_time = rospy.Time.now() + rospy.Duration(duration)
        while rospy.Time.now() < end_time and not rospy.is_shutdown():
            self.pub.publish(self.move_cmd)
            self.rate.sleep()

        # Stop rotation
        self.move_cmd.angular.z = 0
        self.pub.publish(self.move_cmd)

    def stop_robot(self):
        rospy.loginfo("Stopping the robot")
        self.move_cmd.linear.x = 0
        self.move_cmd.angular.z = 0
        self.pub.publish(self.move_cmd)

Here is my CmakeList:

cmake_minimum_required(VERSION 3.0.2)
project(wall_following)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  rospy
  sensor_msgs
  message_generation
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

## Generate services in the 'srv' folder
 add_service_files(
   FILES
   FindWall.srv
 )

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
 generate_messages(
   DEPENDENCIES
   geometry_msgs   sensor_msgs
 )

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
#   cfg/DynReconf1.cfg
#   cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES wall_following
  CATKIN_DEPENDS geometry_msgs rospy sensor_msgs message_runtime
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/wall_following.cpp
# )

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/wall_following_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
 catkin_install_python(PROGRAMS
   scripts/find_wall_service_server.py
   scripts/find_wall_movement.py
   scripts/my_script.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_wall_following.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)

Here is the package.xml:

<?xml version="1.0"?>
<package format="2">
  <name>wall_following</name>
  <version>0.0.0</version>
  <description>The wall_following package</description>

  <!-- One maintainer tag required, multiple allowed, one person per tag -->
  <!-- Example:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <maintainer email="user@todo.todo">user</maintainer>


  <!-- One license tag required, multiple allowed, one license per tag -->
  <!-- Commonly used license strings: -->
  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <license>TODO</license>


  <!-- Url tags are optional, but multiple are allowed, one per tag -->
  <!-- Optional attribute type can be: website, bugtracker, or repository -->
  <!-- Example: -->
  <!-- <url type="website">http://wiki.ros.org/wall_following</url> -->


  <!-- Author tags are optional, multiple are allowed, one per tag -->
  <!-- Authors do not have to be maintainers, but could be -->
  <!-- Example: -->
  <!-- <author email="jane.doe@example.com">Jane Doe</author> -->


  <!-- The *depend tags are used to specify dependencies -->
  <!-- Dependencies can be catkin packages or system dependencies -->
  <!-- Examples: -->
  <!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
  <!--   <depend>roscpp</depend> -->
  <!--   Note that this is equivalent to the following: -->
  <!--   <build_depend>roscpp</build_depend> -->
  <!--   <exec_depend>roscpp</exec_depend> -->
  <!-- Use build_depend for packages you need at compile time: -->
  <!--   <build_depend>message_generation</build_depend> -->
  <!-- Use build_export_depend for packages you need in order to build against this package: -->
  <!--   <build_export_depend>message_generation</build_export_depend> -->
  <!-- Use buildtool_depend for build tool packages: -->
  <!--   <buildtool_depend>catkin</buildtool_depend> -->
  <!-- Use exec_depend for packages you need at runtime: -->
  <!--   <exec_depend>message_runtime</exec_depend> -->
  <!-- Use test_depend for packages you need only for testing: -->
  <!--   <test_depend>gtest</test_depend> -->
  <!-- Use doc_depend for packages you need only for building documentation: -->
  <!--   <doc_depend>doxygen</doc_depend> -->
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>geometry_msgs</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>message_generation</build_depend> 
  <build_export_depend>geometry_msgs</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>sensor_msgs</build_export_depend>
  <build_export_depend>message_runtime</build_export_depend>
  <exec_depend>geometry_msgs</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>sensor_msgs</exec_depend>
  <exec_depend>message_runtime</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

Hi @min.wang.1997.01.13,

It’s the first time I see this error, so I had to use ChatGPT to get an idea of what is happening.

ChatGPT says:

The behavior you’re observing is by design when using catkin_make with Python scripts in a ROS package.

So, to test, I just commented out the following code in the CMakeLists.txt that you provided:

catkin_install_python(PROGRAMS
   scripts/find_wall_service_server.py
   scripts/find_wall_movement.py
   scripts/my_script.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

By turning this into a comment, and rebuilding your workspace with the commands below, your script should work without any problems:

cd ~/catkin_ws

rm build/ devel/ -r

catkin_make

This should solve the problem that you mentioned.


Now, just for the records, the file that was generated on ~/catkin_ws/devel/lib/wall_following/find_wall_movement.py had the following content, responsible for this error:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# generated from catkin/cmake/template/script.py.in
# creates a relay to a python script source file, acting as that file.
# The purpose is that of a symlink
python_script = '/home/user/catkin_ws/src/wall_following/scripts/find_wall_movement.py'
with open(python_script, 'r') as fh:
    context = {
        '__builtins__': __builtins__,
        '__doc__': None,
        '__file__': python_script,
        '__name__': __name__,
        '__package__': None,
    }
    exec(compile(fh.read(), python_script, 'exec'), context)
1 Like

Hi ralves, thanks a lot! :blush:

Is that because scripts/find_wall_movement.py is not a script at that moment but a module I would like to import?
I am confused about this catkin_install_python(). When shall we add the Python scripts to it? Why does the service work without adding scripts/find_wall_service_server.py here (we commented out this line as well)?

My ChatGPT is dead for the whole day. :joy: Lucky you.

Hi @min.wang.1997.01.13,

I don’t understand this error very well, but I never used catkin_install_python for Python scripts.

If we don’t use catkin_install_python, Python just finds it in the src folder, instead of looking inside devel/lib/package_name.

My advice (at least for ROS 1) is:

  1. If it’s a Python file, don’t add it to CMakeLists.txt

But if it’s a ROS 2, then you may need to add it.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.