Python Forum
Error when refering to class defined in 'main' in an imported module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when refering to class defined in 'main' in an imported module
#1
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'.
Reply
#2
Yes create it in one module and import it from that module into anywhere you want to use it.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [newbie] Why is a module imported twice? Winfried 3 1,783 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  main libvlc error: stale plugins cache: schascheck 2 1,958 Dec-27-2020, 05:24 PM
Last Post: schascheck
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 540 Dec-14-2020, 05:45 PM
Last Post: karabakh
Star NameError – function doesn't recognize imported modules Sir 4 777 Dec-01-2020, 06:36 AM
Last Post: Sir
  error in class non_name092 1 635 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  "Class already defined" while using typings. DreamingInsanity 0 703 Aug-19-2020, 10:43 AM
Last Post: DreamingInsanity
  How to assign a module to a variable even if it's not defined? mandaxyz 5 996 Aug-12-2020, 10:34 PM
Last Post: snippsat
  name error "name"is not defined MaartenRo 1 823 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 799 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 861 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020