You are not logged in.

#1 2015-08-10 23:39:03

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

ImportError: cannot import name 'Ui_Dialog' [SOLVED]

I try to achieve a tutorial pyqt but some reason, i can't succeed to compile without
having the message

/usr/bin/python3.4 /home/xxxxxx/PycharmProjects/NewOne/Tryagain.py
Traceback (most recent call last):
  File "/home/xxxxxx/PycharmProjects/NewOne/Tryagain.py", line 3, in <module>
    from form import Ui_Dialog
  File "/home/xxxxxx/PycharmProjects/NewOne/form.py", line 3, in <module>
    from form import Ui_Dialog
ImportError: cannot import name 'Ui_Dialog'

Process finished with exit code 1

Last edited by j2lapoin (2015-08-11 20:49:12)

Offline

#2 2015-08-11 00:01:20

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Are you sure that pyqt4 is installed on your system?  Check the output of pacman -Ss pyqt 
Also, Be aware that that tutorial is written in Python2.

    def OK(self):
        print 'OK pressed.'

Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2015-08-11 00:18:23

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Thanks for the information, I been looking for pyqt apckages (warning: pyqt4-common-4.11.4-1 is up to date -- reinstalling)(python2-pyqt4-4.11.4-1)
they have been installed. But still the same error. I'm wonder if it could be a file name ui_dialog or sort that was somewhere missing. Thanks. maybe it's Qt fault.

ewaller wrote:

Are you sure that pyqt4 is installed on your system?  Check the output of pacman -Ss pyqt 
Also, Be aware that that tutorial is written in Python2.

    def OK(self):
        print 'OK pressed.'

Offline

#4 2015-08-11 02:11:06

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Can you post your form.py that you generated from form.ui using pyuic4  ?


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2015-08-11 02:19:19

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

both form do the same thing. the one on the tutorial site and mine. i send them.

Offline

#6 2015-08-11 02:26:45

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

the first that i must attached to my file...

import sys
from PyQt4 import QtCore, QtGui
from form import Ui_Dialog
 
 
class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyDialog()
    myapp.show()
    sys.exit(app.exec_())

my file after conversion

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 250, 88, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(140, 220, 131, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 40, 171, 151))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/ux.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.label.setText(_translate("Dialog", "This is my first label", None))

Offline

#7 2015-08-11 02:40:36

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Using your file, on my system:

ewaller@turing ~/test 1017 %python
Python 3.4.3 (default, Mar 25 2015, 17:13:50) 
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from form import Ui_Dialog
>>> quit()
ewaller@turing ~/test 1018 %python2
Python 2.7.10 (default, May 26 2015, 04:16:29) 
[GCC 5.1.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from form import Ui_Dialog
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "form.py", line 9, in <module>
    from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4
>>> import form
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "form.py", line 9, in <module>
    from PyQt4 import QtCore, QtGui
ImportError: No module named PyQt4
>>> quit()
ewaller@turing ~/test 1019 %pacman -Qs pyqt
local/pyqt4-common 4.11.4-1
    Common PyQt files shared between python-pyqt4 and python2-pyqt4
local/python-pyqt4 4.11.4-1
    A set of Python 3.x bindings for the Qt toolkit
ewaller@turing ~/test 1020 %ls -l
total 16
-rw-r--r-- 1 ewaller ewaller 5330 Aug 10 19:30 form.py
-rw-r--r-- 1 ewaller ewaller 5168 Aug 10 19:34 form.pyc
drwxr-xr-x 2 ewaller ewaller   16 Aug 10 19:32 __pycache__
ewaller@turing ~/test 1021 %

Edit: Let's try that again hmm

Edit2:  TLDR, it works for Python3, not for Python2.  That is because I've only the Python3 version of PyQt installed.

Last edited by ewaller (2015-08-11 02:45:06)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#8 2015-08-11 02:52:29

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

from my side nothing has changed:

/usr/bin/python2.7 /home/j2lapoin/PycharmProjects/NewOne/form.py
Traceback (most recent call last):
  File "/home/j2lapoin/PycharmProjects/NewOne/form.py", line 3, in <module>
    from form import Ui_Dialog
  File "/home/j2lapoin/PycharmProjects/NewOne/form.py", line 3, in <module>
    from form import Ui_Dialog
ImportError: cannot import name Ui_Dialog

Process finished with exit code 1

Wonder if that matter that the version is 2 instead of 3. I mean my problem seem more likely to be a package missing, but maybe you will prove me wrong.

Offline

#9 2015-08-11 03:02:17

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Is your current directory /home/j2lapoin/PycharmProjects/NewOne/ when you run that command?  It has to be, or form.py has to be in your Python PATH (unlikely)

Well, first, a sanity check.  Do a ls -l just to prove that you are in the right directory smile
Then, do a pacman -Qs pyqt and let's see what pyqt packages you've.
Then, run the Python3 interpreter -- python and then do an import form


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#10 2015-08-11 03:16:30

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

ewaller wrote:

Is your current directory /home/j2lapoin/PycharmProjects/NewOne/ when you run that command?  It has to be, or form.py has to be in your Python PATH (unlikely)

Well, first, a sanity check.  Do a ls -l just to prove that you are in the right directory smile

[j2lapoin@localhost NewOne]$ pwd
/home/j2lapoin/PycharmProjects/NewOne
[j2lapoin@localhost NewOne]$ ls -l
total 116
-rw-r--r-- 1 j2lapoin users   489 Aug 10 20:45 example1.py
-rw-r--r-- 1 j2lapoin users   696 Aug 10 20:47 example2.py
-rw-r--r-- 1 j2lapoin users   935 Aug 10 20:55 example3.py
-rw-r--r-- 1 j2lapoin users  2154 Aug 10 22:48 form.py
-rw-r--r-- 1 j2lapoin users  3018 Aug 10 22:48 form.pyc
-rw-r--r-- 1 j2lapoin users  1290 Aug 10 18:44 form.ui
-rw-r----- 1 j2lapoin users  2578 Aug 10 21:07 helloworld2.py
-rw-r----- 1 j2lapoin users  3093 Aug 10 21:05 helloworld.py
-rw-r--r-- 1 j2lapoin users  1939 Aug 10 19:01 NewExample.py
drwxr-xr-x 2 j2lapoin users  4096 Aug 10 19:31 __pycache__
-rw-r--r-- 1 j2lapoin users  2420 Aug 10 19:52 Tryagain.py
-rw-r--r-- 1 j2lapoin users 57694 Aug 10 20:52 web.png
-rw-r--r-- 1 j2lapoin users   856 Aug 10 20:54 web.xcf

Then, do a pacman -Qs pyqt and let's see what pyqt packages you've.

[j2lapoin@localhost NewOne]$ pacman -Qs pyqt
local/pyqt4-common 4.11.4-1
    Common PyQt files shared between python-pyqt4 and python2-pyqt4
local/python-pyqt4 4.11.4-1
    A set of Python 3.x bindings for the Qt toolkit
local/python2-pyqt4 4.11.4-1
    A set of Python 2.x bindings for the Qt toolkit

Then, run the Python3 interpreter -- python and then do an import form

don't understand this request, i mean what you want. do you want me to add import form and to compile with python 3.4?

Offline

#11 2015-08-11 03:20:19

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Like this:

ewaller@turing ~/test 1024 %python
Python 3.4.3 (default, Mar 25 2015, 17:13:50) 
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import form
>>> dir(form)
['QtCore', 'QtGui', 'Ui_Dialog', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_encoding', '_fromUtf8', '_translate']
>>> quit()
ewaller@turing ~/test 1025 %

Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#12 2015-08-11 03:24:48

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Python 3.4.3 (default, Mar 25 2015, 17:13:50) 
[GCC 4.9.2 20150304 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import form
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/j2lapoin/PycharmProjects/NewOne/form.py", line 3, in <module>
    from form import Ui_Dialog
ImportError: cannot import name 'Ui_Dialog'
>>> dir(form)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'form' is not defined
>>> 

Offline

#13 2015-08-11 03:34:55

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

ewaller@turing ~/test 1025 %ll
total 16
-rw-r--r-- 1 ewaller ewaller 5330 Aug 10 19:30 form.py
-rw-r--r-- 1 ewaller ewaller 5168 Aug 10 19:34 form.pyc
drwxr-xr-x 2 ewaller ewaller   16 Aug 10 19:32 __pycache__
ewaller@turing ~/test 1026 %

Why is my form.py more than twice the size of yours?

does your form.py not look like this?

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 250, 88, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(140, 220, 131, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 40, 171, 151))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/ux.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.label.setText(_translate("Dialog", "This is my first label", None))# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 250, 88, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(140, 220, 131, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 40, 171, 151))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/ux.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.label.setText(_translate("Dialog", "This is my first label", None))# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 250, 88, 27))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(140, 220, 131, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(120, 40, 171, 151))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/ux.jpg")))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "PushButton", None))
        self.label.setText(_translate("Dialog", "This is my first label", None))

Last edited by ewaller (2015-08-11 03:36:00)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#14 2015-08-11 03:42:50

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

mine has the quote from the author of pyqt tutoring.  based on the tutorial we must add this section above.

import sys
from PyQt4 import QtCore, QtGui
from form import Ui_Dialog
 
 
class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyDialog()
    myapp.show()
    sys.exit(app.exec_())

Offline

#15 2015-08-11 04:28:18

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,211

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

That file is supposed to be gui.py

the tutorial you linked wrote:

Running the file does nothing. Create a new file called  gui.py

import sys
from PyQt4 import QtCore, QtGui
from form import Ui_Dialog


class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyDialog()
    myapp.show()
    sys.exit(app.exec_())

form.py is generated using the pyuic4 tool.   You should have two .py files, gui.py and form.py.  It looks like you overwrote form.py with what should be in gui.py.  When you load form.py, it tries to import form.py -- that won't work.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#16 2015-08-11 20:48:01

j2lapoin
Banned
Registered: 2013-06-28
Posts: 102

Re: ImportError: cannot import name 'Ui_Dialog' [SOLVED]

Exactly brillant! I did put the code into gui.py and that finally resolve my problem.  Thank you for your help and [solved] is this problem.


ewaller wrote:

That file is supposed to be gui.py

the tutorial you linked wrote:

Running the file does nothing. Create a new file called  gui.py

import sys
from PyQt4 import QtCore, QtGui
from form import Ui_Dialog


class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyDialog()
    myapp.show()
    sys.exit(app.exec_())

form.py is generated using the pyuic4 tool.   You should have two .py files, gui.py and form.py.  It looks like you overwrote form.py with what should be in gui.py.  When you load form.py, it tries to import form.py -- that won't work.

Offline

Board footer

Powered by FluxBB