Background on the question: This is a previous PyQt project I am working on and trying to start the GUI. I have set an Anaconda Environment with Python 2.7 and used PyQt4. The Error is :-
File "gui/gui.py", line 26, in <module>
from qtpy.QtCore import (Qt, QFileSystemWatcher, QSettings, pyqtSignal)
ImportError: cannot import name pyqtSignal
Code :-
enter #import qt
from qtpy import QtCore, QtWidgets, QtGui, PYQT4 #changed from PYQT5
from qtpy.QtCore import (Qt, QFileSystemWatcher, QSettings, pyqtSignal)
Even after trying to setup the environment and other aspects to the best of my ability, I am unable to pinpoint why this error still pops up.Tried on Mac, it errors out similarly even on Ubuntu. Does anyone have an idea how to tackle this?
You're using qtpy rather than PyQt4 directly. According to Don't delete QtCore.{pyqtSignal,pyqtSlot,pyqtProperty} · Issue #76 · spyder-ide/qtpy · GitHub, they deliberately ditched PyQt-specific names like pyqtSignal and instead rename them upon import to generic names like Signal for uniformity. They comment that these names follow Qt5's naming scheme.
So you should just
from qtpy.QtCore import Qt, QFileSystemWatcher, QSettings, Signal
and rename all pyqtSignal to Signal elsewhere in your code.
Related
I have PyQt5 Installed and I created a GUI and edited it using Sublime Text, Now I want to use PyCharm and I saw posts that you don't have to configure PyQt5, it is automatically detected by PyCharm.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
from mainpy import kitcode
PyQt5, QtCore, QtGui, QtWidgets, PyQt5.QtWidgets, PyQt5.QtGui, QPixmap, mainpy and kitcode are all in red underline.
And when I run it, I get the error
Traceback (most recent call last):
File "C:/Users/username/Desktop/folder/PythonThesis/mainpy.py", line 9, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: No module named 'PyQt5'
Process finished with exit code 1
any help would be appreciated and sorry if this is basic, I am still new to python. Thank you very much.
did you fix this problem?
You may need to tell PyCharm which Python do you want to use for your current project. You can do this by configuring Python interpreter from Settings/Preferences/Project Interpreter. Be careful to find the proper Python with PyQt5. You can find more information on the official website. An extra tip is you can edit run configuration, including Python interpreter for the next run, before running projects by using the shortcut Shift+Alt+F10.
This is the simple line of code for creating the Button Object
btn = QtGui.QPushButton('Button')
and gives out this error
AttributeError: module 'PyQt5.QtGui' has no attribute 'QPushbutton'
These are my imports
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
I am running compatible versions of Python(3.5.2)32Bit and PyQt5 32bit
I tried QtGui.QLabel also and no luck.
PyQt5 is not compitible with PyQt4, so it's very unlikely that you'll be able to run a PyQt4 application with PyQt5 without making some changes. For details, see: Differences Between PyQt4 and PyQt5.
As to the specific issue, try:
btn = QtWidgets.QPushButton('Button')
I'm developping a small graphic application using Python 3 and PyQt5.
On the first computer I use, where only PyQt5 is installed, everything in my code is fine. But when I want to run my code on my other laptop, where both PyQt4 and PyQt5 are installed, I get the following error:
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
Python interpreter locates the error in the file "ViewWindow.py", called from the main file.
As I have both PyQt4 and PyQt5 on this laptop, and because I can't uninstall PyQt4 (it would be too easy...), I wonder if it's possible to force use of PyQt5.QtCore, or something else to avoid this problem.
My configuration on this laptop: Debian 8, Python3.4, PyQt4 and 5 (without special configuration, installed from Debian repos), IDE = Spyder.
I put there first lines of my files main.py and ViewWindow.py.
# main.py
import sys
import sqlite3
import ViewWindow
from DataWindow import DataWindow
from PyQt5.QtCore import QObject # I tried adding this line, but nothing changed...
from PyQt5.QtWidgets import (QApplication,
QWidget,
QGridLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QTextEdit,
QVBoxLayout
)
class MainWindow(QWidget):
# Some cool stuff
# ViewWindow.py
import sys
import sqlite3
from PyQt5.QtCore import QObject # same thing than above, adding this line doesn't change the output.
from PyQt5.QtWidgets import (QApplication,
QWidget,
QGridLayout,
QLabel,
QPushButton,
QVBoxLayout
)
class ViewWindow(QWidget):
Does someone knows how to make this code run ?
Thanks,
Jerry M.
Edit: I tried to run that script forcing use of Python3, and it worked... It seems that problem comes from iPython3.
Thanks for your help.
A RuntimeError with message
the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
is raised the moment you try to import PyQt5.QtCore while PyQt4.QtCore was already imported before.
This error is raised within SIP, which is used to connect to Qt. Like it states, it's only allowed to have one module claiming to wrap QObject. Thus the error just tells you, that you're using PyQt4 and PyQt5 at once.
So you need to find the module loading PyQt4 to configure it to use PyQt5 instead. Alternatively you could try to put from PyQt5.QtCore import QObject before any other import and hope, that the module, which usually imports from PyQt4, is adaptable and able to use PyQt5 as fallback.
I've just moved from PyQt4 to 5 and I'm having an issue with QtGui. I installed using the 32bit windows installer, not my own build.
when I do:
from PyQt5 import QtGui
I get
class MainWindow(QtGui.QMainWindow, UI.MainUI.Ui_MainWindow):
AttributeError: 'module' object has no attribute 'QMainWindow'
so I tried
from PyQt5.QtWidgets import QtGui
Which results in:
ImportError: cannot import name QtGui
then I tried to change the sys.path according to Pyinstaller: ImportError: cannot import name QtGui work around but it still gives me the same
ImportError: cannot import name QtGui
Update: It looks like I do in fact import QtGui because when I go in IDLE and try it, it still autocompletes QMovie and a whole bunch of other attributes. Is there any reason QMainWindow just wouldn't be in there? (It's not, neither is QDialog and they seem important)
Assuming everything was installed correctly, you need to adjust your imports slightly to port from PyQt4 to PyQt5.
The main GUI elements are in the QtWidgets module, whilst the more basic GUI elements are in QtGui. See the Qt modules page for more details.
The example code needs to be changed to something like:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow, UI.MainUI.Ui_MainWindow):
...
For more details on porting from PyQt4 to PyQt5, see: Differences Between PyQt4 and PyQt5.
I'm running Python 2.6.6 on Ubuntu 10.10.
I understand that we can import a module and bind that module to a different name, e.g.
import spam as eggs
also,
from eggs import spam as foo
My problem is that when running the PySide examples, the following import code does not run:
import PySide as PyQt4
from PyQt4 import QtCore, QtGui
It generates an import error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4
Clearly, according to the Python interpreter the above code is incorrect, my question is why is it incorrect or rather why doesn't this work?
import and from are a special syntax.
They look for a module name, which means a file in sys.path which starts with the module name.
And it seems like you don't have PyQt4 installed, so it will fail.
The fact that you have a variable called PyQt4 in your namespace after running import PySide as PyQt4 does not change anything, Python is still looking for an actual module called PyQt4 when you do from PyQt4 import QtCore, QtGui.
Try doing
import PySide as PyQt4
QtCore = PyQt4.QtCore
QtGui = PyQt4.QtGui
or
import PySide as PyQt4
from PySide import QtCore, QtGui
That should be equivalent.
I just installed PySide and was doing a tutorial where all the examples used PyQt4. I got tired of changing the imports from PyQt4 to PySide so I just made a symlink in my site-packages, using the following steps:
1) There's surely a better way but I found where my python packages were installed by opening a shell and running python, then at the interactive interpreter typed:
>>> import sys
>>> print sys.path
2) I then found PySide in one of the directories and cd'd to it (n.b. It's at /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages if you're using the macports PySide install for python 2.7 on Mac OSX Leopard 10.5.8).
3) Then I made a symlink with ln, in my case I had to use sudo:
sudo ln -s PySide PyQt4
That's it, now I can just use:
from PyQt4 import QtGui
as normal - happy days!
Obviously, if you ever want to install PyQt4 you should rm the PyQt4 symlink first.
Another caveat: What I've described above may well be wrong/bad in many ways - I am no expert at Python installs but so far it's ok for me. YMMV so use at your own risk. Hopefully someone will comment soon to say "no, very bad!" or ideally "yeah don't sweat it, we cool.."