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

In this post, we will see how to resolve Python classes imported in .kv file are not identified and throws error

Question:

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
then I get below error

AttributeError: module ‘libs.baseclass’ has no attribute ‘main_screen’


Here is my main.kv file
My project structure is below:
Could you please help me identify the issue.

Best Answer:

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.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com