ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Using rosrun breaks python3 import

asked 2021-08-22 04:54:42 -0500

jonasfovea gravatar image

Hey there,

I'm writing a ROS node in Python and I want to import Classes from a file in the same directory as my node.

When executing the file wit python3, the imports are fine. But when I execute the file using $ rosrun mypackage Node.py, I get an Error ImportError: cannot import name 'Com' from 'Com' (/home/myname/catkin_ws/devel/lib/mypackagename/Com.py) (I'm importing the Class Com from the file Com.py).

The Error message shows that it's importing from the /devel/lib/mypackagename/ directory.

So my question is, how do I tell ROS to import the file from the same directory, my node is in?

I'm using ROS Noetic 1.15.11 The node file itself and the file Com.py is in the directory ~/catkin_ws/src/mypackagename/scripts I'm importing the class as follows: from Com import Com

Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-09-30 08:58:16 -0500

Jani_C gravatar image

In your CMakeLists.txt look for the following block (it may look a little different given the dependencies)

catkin_package(
# INCLUDE_DIRS include
# LIBRARIES <your_package_name>
# CATKIN_DEPENDS roscpp rospy 
# DEPENDS system_lib
)

Uncomment the LIBRARIES line and the block should look like as follows.

catkin_package(
# INCLUDE_DIRS include
LIBRARIES <your_package_name>
# CATKIN_DEPENDS roscpp rospy 
# DEPENDS system_lib
)

Hope this helps.

edit flag offensive delete link more
0

answered 2021-08-22 11:26:29 -0500

Mike Scheutzow gravatar image

The phrase from Com tells python to find a Python Module named Com. To be clear, it does not mean "find a file named Com.py". The statement you probably want is:

import Com.Com as Com

but I find this statement confusing to read, so what I tend to do is name the python file in lower case, and use the full name down in the code:

import com
c = com.Com()
edit flag offensive delete link more

Comments

You're right, the naming isn't optimal, but that was not my decision.

Nevertheless, the import works just fine when I execute it with python3, so that's not the Problem.

And as I mentioned, the interpreter used by rosrun is searching in another directory, what causes my problem.

jonasfovea gravatar image jonasfovea  ( 2021-08-23 02:25:08 -0500 )edit

Any solution for this? I am also facing the same issue when using rosrun.

HappySamuel gravatar image HappySamuel  ( 2022-03-02 03:12:21 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-08-22 04:54:42 -0500

Seen: 768 times

Last updated: Aug 22 '21