Import outside toplevel (bad or acceptable?)

Hello! :slight_smile:

I have a need (since I’m very pedantic) to fix a thing that Pylint is complaining about in my code.

superchess.py
Line: 59
pylint: import-outside-toplevel / Import outside toplevel (source.window.main_window) (col 8)

Please look at my code on GitHub HERE and tell me how can I fix that nagging Pylint message.

I’ve tried everything and I can’t have my code working other than importing main_window inside an if block. I read on some forum that having imports like I do indicate a code smell.

All tries other than what I have now give me this error message:
QWidget: Must construct a QApplication before a QWidget

Any help or suggestion would be very welcome. Thanks in advance!

Hi,
We don’t have access to your code on Github (error 404)

Yeah, sorry about that. The link works now.

So, what can I do to fix my problem? (The line in question is now 64, not 59.)

The problem is that the import of main_window is creating a QWidget – you’re creating an instance of that window object in that file, which you’re then importing – normally I would import the class, and create the instance of it in this file, e.g.

            from source.window import MainWindow  # or whatever it's called
            main_window = MainWindow()
            main_window.show()

That will postpone the creation of the widget til this point, where the QApplication instance exists.

1 Like