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
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 515 Nov-23-2023, 02:53 PM
Last Post: rob101
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  "Name is not defined" when running a class lil_e 6 3,766 Jan-12-2023, 11:57 PM
Last Post: lil_e
  My code displays too much output when importing class from a module lil_e 4 1,103 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,777 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  No module found when I run a main.py tomtom 2 1,411 Jul-20-2022, 09:24 AM
Last Post: tomtom
  Can a module tell where it is being imported from? stevendaprano 3 1,140 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,044 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,283 Mar-13-2022, 03:29 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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