0

In my gui.py module I have:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
        ...

How do I correctly import everything from that module in my main.py without from gui import * Should I again use in my from PyQt5 import QtCore ... or from gui import QtCore ...?

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41
  • 2
    You should treat every python module as independent, each one with its requiring imports to have it working. Python does then smart stuff to cache modules and avoid compiling them twice. So short answer include the PyQt import in all the modules that use it. – Imanol Luengo Jul 07 '17 at 10:31

1 Answers1

0

In general, you shouldn't import stuff from a module which itself has only imported it:

# gui.py
from PyQt5 import QtCore, QtGui, QtWidgets

def some_function():
    pass

you should then do:

# main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from gui import some_function

The only exception are __init__.py files that aggregate modules from its submodules for convenience reasons:

# some_module/__init__.py
from .submodule import some_function
from .other_submodule import some_other_function

and then

# main.py
from .some_module import some_function, some_other_function

Since the're not modules that are provided by gui you should just import them directly from PyQt5.

Nils Werner
  • 34,832
  • 7
  • 76
  • 98