Ask Your Question
0

Using rosrun breaks python3 import

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

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

1 Answer

Sort by ยป oldest newest most voted
0

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

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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 49 times

Last updated: Aug 22