Search code examples
pythonkivykivy-languagekivymdbuildozer

Python classes imported in .kv file are not identified and throws error


Pardon me if I'm asking a naive question as I'm new to Kivy.

In my Kivy app I'm importing few python classes and kv widgets. These run fine on my local but throws error when I convert the app to apk and try opening on my android. Here is my error while opening python app

Unable to import SplashScreen from libs.baseclass.main_screen.SplashScreen

If I change the import statement to

#: import SplashScreen libs.baseclass.main_screen

then I get below error

AttributeError: module 'libs.baseclass' has no attribute 'main_screen'

Here is my main.kv file

#:kivy 2.1.0

#: import SplashScreen libs.baseclass.main_screen.SplashScreen
#: import LoginScreen libs.baseclass.main_screen.LoginScreen
#: import SignupScreen libs.baseclass.main_screen.SignupScreen
#: import TopicsScreen libs.baseclass.main_screen.TopicsScreen
#: import SubTopicsScreen libs.baseclass.main_screen.SubTopicsScreen

#: include libs/kvs/splash_screen.kv
#: include libs/kvs/login_screen.kv
#: include libs/kvs/signup_screen.kv
#: include libs/kvs/topics_screen.kv
#: include libs/kvs/sub_topics_screen.kv


ScreenManager:
    SplashScreen:
    LoginScreen:
    SignupScreen:
    TopicsScreen:
    SubTopicsScreen:

My project structure is below:

    Myapp
      - libs
        - baseclass
          - main_screen.py (this files contains all the classes imported in main.kv file )
        - kvs
           All the .kv files defining screens are here.
      - main.kv
      - main.py

Could you please help me identify the issue.


Solution

  • Looking the code shared by you, it seems like the issue might be coming from in name of module i.e. main_screen.

    As per the PEP8 documentation

    Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

    Try removing the underscore from module name, something similar to mainscreen.py. This might work.

    Other approach can be creating init file in libs.baseclass and import the classes from main_screen.py in this init file. As a result in .kv file you will have to import classes from libs.baseclass which does not have underscore. Although this method is not tested but worth to give a try.