I am executing this example file for matplotlib and I am getting the following error while exiting the mainwindow normally.
Fatal Python error: deallocating None
I have only changed the following lines, because i wanted to use PySide2 instead of PyQt.
Original:
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
New:
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
matplotlib.rcParams['backend.qt5']='PySide2'
from PySide2 import QtCore, QtWidgets
The program otherwise runs perfectly, it just crashes when i close it, which is a bit irritating.
My environment consists of:
Python 3.6.4
PySide2 5.6.0a1
MatplotLib 2.2.2
Any ideas?
Refer to this post https://stackoverflow.com/a/49712824/6704496, it appears a not up-to-date version bug. Please download the newest wheel http://download.qt.io/snapshots/ci/pyside/5.9/latest/.
Related
I have been using the QT designer tool which saves GUIs as a XML template. PySide2 is able to covert this to a Python Class file.
Utilizing the tool for an XML that includes QWebEngineView
pyside2-uic GUI_NEW.ui > ui_main.py
The first few lines of the ui_main.py call for
################################################################################
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from QWebEngineView import QWebEngineView
running the file that utilizes this, results in:
ModuleNotFoundError: No module named 'QWebEngineView'
So far I have tried
Trying PySide6 over PySide 2
Replaced the import line with
from PySide2.QtWebEngineWidgets import QWebEngineView
The Application runs but completely hangs
pip install PyQtWebEngine
pip install PyQt5==5.11.3
Cleaning up, installing all references to Qt (Pyside, PyQT, etc), and re-installing
Attempted all the above on python 3.6 - 3.8
Here is another option, you don't have it.
Then try this option:
from PyQt5 import QtWebEngineWidgets
That is, an example of a string:
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
But, unfortunately Qt, PyQt and PySide should never be used together. Most problems occur when trying to re-display a widget created with a binding with another one created with a different binding.
The solution is simple: you either use PyQt5 and use QtWebEngineWidgets, or PySide2.
As a last resort, try installing a newer version (5.12)(PyQt5) and install PyQtWebEngine separately.
Ultimately I stuck to PySide2 (option 2), creating a new project specific python environment that did not involve PyQt5. This prevented the application from hanging with zero console errors.
I have a slight modified version of this example from matplotlib: https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html
The only thing changed is the imports since I am using PySide2, so the imports looks like this:
from PySide2 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
This works fine when running the code in pycharm, or running the scrips by itself, however after an .exe is created with PyInstaller I get the following error:
TypeError: 'PySide2.QtWidgets.QBoxLayout.addWidget' called with wrong argument types:
PySide2.QtWidgets.QBoxLayout.addWidget(FigureCanvasQTAgg)
Supported signatures:
PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget, int=0,
PySide2.QtCore.Qt.Alignment=Default(Qt.Alignment))
PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget)
It seems as the FigureCanvasQtAgg is no longer recognized as a QWidget, so it can't be added to the layout.
I've tried adding these lines to suggest pyside as suggested here:
os.environ["QT_API"] = "PySide2"
matplotlib.use('Qt5Agg')
matplotlib.rcParams['backend.qt5']='PySide2'
However that does not change the error message of the exe. In pycharm it still runs fine.
EDIT: It seems this is some problem with PySide2+PyInstaller, after replacing this line:
from PySide2 import QtCore, QtWidgets
with this line:
from PyQt5 import QtCore, QtWidgets
It works even after using PyInstaller.
But I want to use PySide2 instead of PyQt5, anyone know a way to solve this?
This thread is a bit old, however, as it was the first hit when I googled for the problem, and this solution is not yet mentioned, I thought I share it:
I had the issue the other way around: I had adapted this live plotting solution to use PySide2, but naturally first tried it with PyQt5. After adaption to PySide2, running the single script failed with the same error as mentioned by OP. However, when importing the window class to another module and creating it there, everything worked fine.
What worked for me was removing PyQt5 from the environment, i.e.
pip uninstall PyQt5
After that, running the script alone worked again.
EDIT: I just revisited my code, and found that changing the order of imports, so that PySide2 is imported prior to matplotlib also did the trick. So it seems that if neither PyQt5 nor PySide2 is already imported, matplotlib checks if PyQt5 is installed and if so, uses this as backend.
Not working:
import matplotlib
from matplotlib.animation import TimedAnimation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
from PySide2 import QtWidgets
working:
from PySide2 import QtWidgets
import matplotlib
from matplotlib.animation import TimedAnimation
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
For PySide2, I have a similar problem when I use the version PySide 5.13.
It is however, working fine for PySide version 5.6.
Interestingly, the issue looks like with the matplotlib wrapper. It seems there is a change in the wrapper for FigureCanvasQTAgg. The one that is working, the wrapper is: Shiboken.ObjectType. While the one that is not working, the wrapper is: sip.wrappertype.
I had the same problem, while using PySide6, on Ubuntu 20.04.
For reference, here is the error I was getting:
TypeError: 'PySide6.QtWidgets.QGraphicsScene.addWidget' called with wrong argument types:
PySide6.QtWidgets.QGraphicsScene.addWidget(LivePlotFigCanvas)
Supported signatures:
PySide6.QtWidgets.QGraphicsScene.addWidget(PySide6.QtWidgets.QWidget, PySide6.QtCore.Qt.WindowFlags = Default(Qt.WindowFlags))
Following worked:
Import all PySide6 packages before importing matplotlib packages
Uninstall and install matplotlib
This error doesn't seem to be related to PyInstaller.
I had the same problem. This worked for me
conda uninstall matplotlib
pip install matplotlib
I am trying to use PyQt4 in pycharm. My code works perfectly in run mode, but in debug mode, when I try to import PyQt4 I get the following error:
"RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class"
This happens even w/ very barebones code:
from PyQt4 import QtGui, QtCore
print('cheese')
(thus, this is different from previous PyQt4 Pycharm problems w. Matplotlib)
Clearly, the PyCharm debugger is using PyQt5 (this can be seen by calling sys.modules['PyQt5']). How can I 'un-import' PyQt5, or at least prevent the collision?
Also: I tried importing differently to include explicit dependencies, but this also gives error:
import PyQt4 as pp
pp.QtGui
AttributeError: module 'PyQt4' has no attribute 'QtGui'
Thanks!
you can go to Settings>Build,Execution,Deployment>Debugger>Python Debugger>PyQt compatible:
Select PyQt4.
Try going to File > Settings > Project > Project Interpreter. Edit your current interpreter, or create a new one, and remove PyQt5 from the list that shows up.
I just began to learn PyQt5 in python and the window which opens becomes not responding
import sys
import PyQt5
from PyQt5 import QtWidgets
from PyQt5 import QtCore
x=QtWidgets.QApplication(sys.argv)
y=QtWidgets.QWidget()
y.show()
Try adding the following line to the end of your code:
sys.exit(x.exec_())
I would also recommend following a tutorial such as this one as PyQt does have a framework which needs to be followed closely. (http://zetcode.com/gui/pyqt5/firstprograms/)
I want to use qt with python.
"import qt" return me :"ImportError: No module named qt". I already instaled pyqt.
what I hve to install in order to activate "import qt"
Thank You
The current PyQt comes in the PyQt4 package, which has several modules. You almost always need to start with:
from PyQt4 import QtCore
from PyQt4 import QtGui
Add other imports as necessary if you need additional sub-modules of PyQt (like QtNetwork, QtSql etc).
Tip for the future: find the lib/site-packages library of your Python installation and look at the packages in there if you're not sure about the name after installing.
Also, arm yourself with a PyQt tutorial - any decent one covers this in the first few paragraphs.
import PyQt4 works for me fine.