Python Forum

Full Version: Error when refering to class defined in 'main' in an imported module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

In my 'main.py' I have this:

class ScalableLabel(Label):
    pass

from sharebox_ios_etc import ShareBox
And in 'sharebox_ios_etc.py' I have this:

class ShareBox(ScalableLabel):
But that line produces this error:

Error:
Traceback (most recent call last): File "/mnt/4AF15A0435E762B4/mypython/GeoESP/main.py", line 70, in <module> from sharebox_ios_etc import ShareBox File "/mnt/4AF15A0435E762B4/mypython/GeoESP/sharebox_ios_etc.py", line 15, in <module> class ShareBox(ScalableLabel): NameError: name 'ScalableLabel' is not defined
So how can I refer to a class defined in the main module in an imported module?
Do I have to but "class ScalableLabel(Label):" in a third 'common.py' module and import that in both of the others?
In that case I guess I would have to import Label in 'common.py'.
Yes create it in one module and import it from that module into anywhere you want to use it.
(Apr-13-2021, 07:00 PM)Yoriz Wrote: [ -> ]Yes create it in one module and import it from that module into anywhere you want to use it.

Thank you for your reply! But it turns out that this is not really necessary...
The problem I was trying to solve can be solved in another way.

I am using the Kivy framework, so my 'main.py' file "imports" a file written in the Kivy language:

Builder.load_file('MyTabbedPanel.kv')
The real problem was that this was placed AFTER:

from sharebox_ios_etc import ShareBox
So I just had to put them in this order:

Builder.load_file('MyTabbedPanel.kv')

from sharebox_ios_etc import ShareBox