Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I can't import .ui file: No module named error
Forum Updated to NodeBB v4.3 + New Features

I can't import .ui file: No module named error

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hernancrespo89
    wrote on 29 Jan 2020, 16:54 last edited by hernancrespo89
    #1

    Hi,
    i created a GUI using QtDesigner. I have been trying to import it using loadUi But i see this error:

    ImportError: No module named 'histogram'
    

    But my .py file and .ui file are on the same directory. How does it happen?
    loadhist.png

    And this is my full code:

    from PyQt5.QtWidgets import *
    from matplotlib.backends.backend_qt5agg import FigureCanvas
    from matplotlib.figure import Figure
    
    
    class Histogram(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.canvas = FigureCanvas(Figure())
    
            vertical_layout = QVBoxLayout()
            vertical_layout.addWidget(self.canvas)
    
            self.canvas.sumbu1 = self.canvas.figure.add_subplot(111)
            self.canvas.figure.set_facecolor("xkcd:wheat")
            self.setLayout(vertical_layout)
    
    
    # main_histogram.py
    import cv2
    import numpy as np
    from PyQt5.QtWidgets import *
    from PyQt5.uic import loadUi
    from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
    from PyQt5.QtWidgets import QDialog, QFileDialog
    from PyQt5.QtGui import QIcon, QPixmap, QImage
    
    import numpy as np
    
    fname = ""
    
    
    class Display_Histogram(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            loadUi("histogram.ui", self)
    
            self.setWindowTitle("Image Histogram")
            self.pbImage.clicked.connect(self.display_histogram)
            self.addToolBar(NavigationToolbar(self.widgetDisplay.canvas, self))
    
        def display_histogram(self):
            global fname
            fname = QFileDialog.getOpenFileName(self, 'Open file',
                                                'd:\\', "Image Files (*.jpg *.gif *.bmp *.png *.tiff)")
            pixmap = QPixmap(fname[0])
            self.labelImage.setPixmap(pixmap)
            self.labelImage.setScaledContents(True);
    
            self.widgetDisplay.canvas.sumbu1.clear()
            read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
            color = ('b', 'g', 'r')
            for i, col in enumerate(color):
                histr = cv2.calcHist([read_img], [i], None, [256], [0, 256])
                self.widgetDisplay.canvas.sumbu1.plot(histr, color=col, linewidth=3.0)
                self.widgetDisplay.canvas.sumbu1.set_ylabel('Y', color='blue')
                self.widgetDisplay.canvas.sumbu1.set_xlabel('X', color='blue')
                self.widgetDisplay.canvas.sumbu1.set_title('Histogram')
                self.widgetDisplay.canvas.sumbu1.set_facecolor('xkcd:wheat')
                self.widgetDisplay.canvas.sumbu1.grid()
            self.widgetDisplay.canvas.draw()
    
    
    if __name__ == '__main__':
        import sys
    
        app = QApplication(sys.argv)
        ex = Display_Histogram()
        ex.show()
        sys.exit(app.exec_())
    
    J 1 Reply Last reply 29 Jan 2020, 18:44
    0
    • H hernancrespo89
      29 Jan 2020, 16:54

      Hi,
      i created a GUI using QtDesigner. I have been trying to import it using loadUi But i see this error:

      ImportError: No module named 'histogram'
      

      But my .py file and .ui file are on the same directory. How does it happen?
      loadhist.png

      And this is my full code:

      from PyQt5.QtWidgets import *
      from matplotlib.backends.backend_qt5agg import FigureCanvas
      from matplotlib.figure import Figure
      
      
      class Histogram(QWidget):
          def __init__(self, parent=None):
              QWidget.__init__(self, parent)
              self.canvas = FigureCanvas(Figure())
      
              vertical_layout = QVBoxLayout()
              vertical_layout.addWidget(self.canvas)
      
              self.canvas.sumbu1 = self.canvas.figure.add_subplot(111)
              self.canvas.figure.set_facecolor("xkcd:wheat")
              self.setLayout(vertical_layout)
      
      
      # main_histogram.py
      import cv2
      import numpy as np
      from PyQt5.QtWidgets import *
      from PyQt5.uic import loadUi
      from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
      from PyQt5.QtWidgets import QDialog, QFileDialog
      from PyQt5.QtGui import QIcon, QPixmap, QImage
      
      import numpy as np
      
      fname = ""
      
      
      class Display_Histogram(QMainWindow):
          def __init__(self):
              QMainWindow.__init__(self)
              loadUi("histogram.ui", self)
      
              self.setWindowTitle("Image Histogram")
              self.pbImage.clicked.connect(self.display_histogram)
              self.addToolBar(NavigationToolbar(self.widgetDisplay.canvas, self))
      
          def display_histogram(self):
              global fname
              fname = QFileDialog.getOpenFileName(self, 'Open file',
                                                  'd:\\', "Image Files (*.jpg *.gif *.bmp *.png *.tiff)")
              pixmap = QPixmap(fname[0])
              self.labelImage.setPixmap(pixmap)
              self.labelImage.setScaledContents(True);
      
              self.widgetDisplay.canvas.sumbu1.clear()
              read_img = cv2.imread(fname[0], cv2.IMREAD_COLOR)
              color = ('b', 'g', 'r')
              for i, col in enumerate(color):
                  histr = cv2.calcHist([read_img], [i], None, [256], [0, 256])
                  self.widgetDisplay.canvas.sumbu1.plot(histr, color=col, linewidth=3.0)
                  self.widgetDisplay.canvas.sumbu1.set_ylabel('Y', color='blue')
                  self.widgetDisplay.canvas.sumbu1.set_xlabel('X', color='blue')
                  self.widgetDisplay.canvas.sumbu1.set_title('Histogram')
                  self.widgetDisplay.canvas.sumbu1.set_facecolor('xkcd:wheat')
                  self.widgetDisplay.canvas.sumbu1.grid()
              self.widgetDisplay.canvas.draw()
      
      
      if __name__ == '__main__':
          import sys
      
          app = QApplication(sys.argv)
          ex = Display_Histogram()
          ex.show()
          sys.exit(app.exec_())
      
      J Offline
      J Offline
      JonB
      wrote on 29 Jan 2020, 18:44 last edited by JonB
      #2

      @hernancrespo89
      This is way thrown out there on a hunch: if the file named histogram shown in the UI is relevant, why is not named histogram.py?

      And please copy & paste the full text (we can't see the top) of the traceback here (not screenshot).

      H 1 Reply Last reply 29 Jan 2020, 19:39
      0
      • J JonB
        29 Jan 2020, 18:44

        @hernancrespo89
        This is way thrown out there on a hunch: if the file named histogram shown in the UI is relevant, why is not named histogram.py?

        And please copy & paste the full text (we can't see the top) of the traceback here (not screenshot).

        H Offline
        H Offline
        hernancrespo89
        wrote on 29 Jan 2020, 19:39 last edited by hernancrespo89
        #3

        @JonB said in I can't import .ui file: No module named error:

        @hernancrespo89
        This is way thrown out there on a hunch: if the file named histogram shown in the UI is relevant, why is not named histogram.py?

        this is my reference, and there is no histogram.py there.

        And please copy & paste the full text (we can't see the top) of the traceback here (not screenshot).

        Here is the full traceback:

        Traceback (most recent call last):

        File "C:/Users/mustafa/Desktop/histogram6/histogram", line 68, in <module>
          ex = Display_Histogram()
        File "C:/Users/mustafa/Desktop/histogram6/histogram", line 36, in __init__
          loadUi("histogram.ui", self)
        File "C:\Python35\lib\site-packages\PyQt5\uic\__init__.py", line 226, in loadUi
          return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
        File "C:\Python35\lib\site-packages\PyQt5\uic\Loader\loader.py", line 72, in loadUi
          return self.parse(filename, resource_suffix, basedir)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 1000, in parse
          actor(elem)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 807, in createUserInterface
          self.traverseWidgetTree(elem)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
          handler(self, child)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
          self.traverseWidgetTree(elem)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
          handler(self, child)
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 253, in createWidget
          self.stack.push(self.setupObject(widget_class, parent, elem))
        File "C:\Python35\lib\site-packages\PyQt5\uic\uiparser.py", line 217, in setupObject
          obj = self.factory.createQObject(clsname, name, args, is_attribute)
        File "C:\Python35\lib\site-packages\PyQt5\uic\objcreator.py", line 106, in createQObject
          factory = self.findQObjectType(classname)
        File "C:\Python35\lib\site-packages\PyQt5\uic\objcreator.py", line 131, in findQObjectType
          w = module.search(classname)
        File "C:\Python35\lib\site-packages\PyQt5\uic\Loader\qobjectcreator.py", line 115, in search
          module = __import__(mname, {}, {}, (cls,))
        ImportError: No module named 'histogram'
        
        1 Reply Last reply
        0
        • T Offline
          T Offline
          tedburns
          wrote on 18 Jan 2024, 15:36 last edited by
          #4

          I got same issue. The import error comes from the <header> of CustomWidgets in the ui file. For C++ it looks for the *.h file that goes along with the ui file. For PyQt5 it does not resolve to anything.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 18 Jan 2024, 19:52 last edited by
            #5

            @tedburns hi and welcome to devnet,

            The example in the blog wrote that the Python file should have the same name as the class. This implies that the file shall have a .py extension as rightfully noted by @JonB.

            In C++, uic will generate the required code if you want to have a full class (you usually want that) and build it. In Python, you have to follow the same logique and generate Python code out of the .ui file if you want to extend the widget with code.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved