ImportError: No module named ... only happens with one file
I'm using Python 2.7.12 and ROS Kinetic Kame on Ubuntu Linux 16.04 Xenial. I have two Python scripts I'm trying to run for two different nodes. My file structure is:
romi_soccer
bin
jsongwriter
mapper
src
__init__.py
json_grabber.py
image_mapper.py
Inside of jsongwriter
is
#! /usr/bin/env python
import rospy
from json_grabber import JSONGrabber
if __name__=='__main__':
rospy.init_node('json_grabber')
try:
rospy.loginfo('Initializing node...')
jason = JSONGrabber()
except rospy.ROSInterruptException:
pass
And the top of json_grabber.py
includes import roslib
and roslib.load_manifest('json_grabber')
although it worked even before I added that. My CMakeLists.txt
file includes the lines
catkin_install_python(PROGRAMS bin/jsongwriter bin/mapper
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
And my setup.py
is under the romi_soccer
and contains this:
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
d = generate_distutils_setup(
packages=['romi_soccer'],
package_dir={'': 'bin' 'src'}
)
setup(**d)
Now, all of that works. I don't get this error on that. But on mapper
, which is almost identical to jsongwriter
, I get the import error, despite it being in the same directory as jsongwriter
, and the file it's referencing also being in that same directory.
This is what mapper
looks like:
#! /usr/bin/env python
import rospy
from image_mapper import ImageMapper
if __name__=='__main__':
rospy.init_node('image_mapper')
try:
rospy.loginfo('Initializing node...')
map = ImageMapper()
except rospy.ROSInterruptException:
pass
The file it's trying to import, image_mapper.py
, also has the import roslib
and roslib.load_manifest('image_mapper')
. But when I try to rosrun romi_soccer mapper
, I get this error:
Traceback (most recent call last):
File "/home/username/workspace/src/romi_soccer/bin/mapper", line 3, in <module>
from image_mapper import ImageMapper
ImportError: No module named image_mapper
I'm absolutely stumped. I realize this particular error is a common issue, and I've read through all of the forums about this topic, but they all seem to be fixed by something I already did to get jsongwriter
to work. I don't understand why I'd have this error on one file but not on another when the file and the thing it's trying to import are located in exactly the same directories as the one that works.
Other notes: I realize the executable Python scripts are usually in a folder called scripts
and not in bin
but when I tried to change it to a scripts
folder and changed the CMakeLists.txt
and setup.py
accordingly, both files had the import error. So I changed it back to bin
so at least one of the files would work.
Any help is greatly appreciated. I'm fairly new to both Python and ROS, and I have no idea what to do now.
Asked by octolass on 2018-09-22 14:59:01 UTC
Answers
Not sure I understand every aspect of what you have tried and where it fails, but...
Normally it is a good idea to place python code that needs to be reused by other python code in a package, which means in a properly named folder. Preferably named after the ROS package. In your case:
romi_soccer
bin
jsongwriter
mapper
src
romi_soccer
__init__.py
json_grabber.py
image_mapper.py
This makes json_grabber.py and image_mapper.py part of a python package named romi_soccer. When importing from this package you will then do:
from romi_soccer.image_mapper import ImageMapper
And then your bin need not be part of the exported packages:
d = generate_distutils_setup(
packages=['romi_soccer'],
package_dir={'': 'src'}
)
Asked by knxa on 2018-09-23 11:14:24 UTC
Comments
This is what I initially had it as, but I had changed the file structure after I ran into these issues while I was trying to fix it. It always said there was no image_mapper module in romi_soccer. Is there something I need to change to setup py or CMakeList.txt for this?
Asked by octolass on 2018-09-24 07:16:47 UTC
Comments