Robotics StackExchange | Archived questions

Kivy in ROS enviroment throws an error: ImportError: No module named app

I would like to have an graphical interface in Kivy for my robot while ROS will works in background.

First will illustrate the project hierarchy.

rosworkscape                  <-catkinwrokspace
|--src
    |--gui                <---catkinpackage
        |--control
panel
            |--_init.py
            |--graphical
interface.py
        |--srv
            |-- __init.py
            |--LightsToggle.srv
        |--__init
.py
        |--CMakeLists.txt
        |--package.xml
        |--start_method.py

I made a very simple interface in Kivy:

graphical_interface.py

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
import rospy
from gui.srv import LightsToggle

class MainMenuScreen(Screen):
    def __init__(self, **kwargs):
        super (MainMenuScreen, self).__init__(**kwargs)

layer = BoxLayout(orientation='vertical')
btn_lights = Button(text="Lights", size_hint_y=None, size_y=100)
btn_lights.bind(on_press=self.lights_toggle)
layer.add_widget(btn_lights)    

    lights = Label(text="Lights: OFF", font_size='18dp', size_hint=(1, .3), halign='left')       
    lights.bind(size=lights.setter('text_size'))      
    layer.add_widget(lights)

self.add_widget(layer)

def lights_toggle(self, *args):
try:
    rospy.wait_for_service('lights_toggle', timeout = 2.0)
    except rospy.ROSException:
        print("Failed. Is server running?")
    return
try:        
    lights_toggle = rospy.ServiceProxy('lights_toggle', LightsToggle)
    response = lights_toggle()
    print ('Lights are :' + response)
except rospy.ServiceException, e:
    print ('Service call failed: ' + e)

class ControlPanelApp(App):     
    def build(self):
        screen_manager = ScreenManager(transition=SlideTransition())
        screen_1 = MainMenuScreen(name='menu')
        screen_manager.add_widget(screen_1)
        return screen_manager


def graphical_interface(cmd_q):
    ControlPanelApp().run()

And this file is run by start_method.py

from control_panel.graphical_interface import graphical_interface
if __name__ == '__main__':
    graphical_interface()

Now the issue:
1) I run catkinmake in rosworkscape - done successfully.
2) source devel/setup.bash
3) rosrun gui start_method.py ( ! roscore is already running)

The code is ok and it works.

It will fails with:

...somepath/rosworkspace/src/gui/controlpanel/graphical_interface.py", line 6, in from kivy.app import App ImportError: No module named app

If I do not perform

source devel/setup.bash

then the Kivy works! But it fails on importing the

LightsToggle

If I run the code without Kivy just ROS and with service (with source devel/setup.bash) it works as well.

I guess during the source devel/setup.bash is some system path or Python path overwritten but I can't figure it out.

Asked by robopo on 2016-04-19 13:31:28 UTC

Comments

Answers

Finally I found it out. It's not overwritten, it's ok.

It's not a good idea to name your files kivy.py, but even the folders should'nt have the name kivy. I made that mistake and I realized it before writting this question. So I renamed them.

That's why it works without doing the

source devel/setup.bash.

Meanwhile I in my catkin_workspace I ran the catkin_make. And it generated messages to following folder

~/ros_workspace/devel/lib/python2.7/dist-packages

And here was the folder called Kivy and from here it tries to import the Kivy and then it fails.

Be sure you rename everything and delete the rest (especially generated files). :-)

Asked by robopo on 2016-04-24 00:55:46 UTC

Comments