Import Error embedding IPython into PyQt application - python

I have a fully functioning PyQt4 based application that I am now trying to embed an IPython console into for use as a kind of scripting environment.
I've roughly adapted the example from IPython's Github page to fit into my application's module system. However, if any PyQt import happens before the IPython imports
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager
I get the following error:
ImportError:
Could not load requested Qt binding. Please ensure that
PyQt4 >= 4.7 or PySide >= 1.0.3 is available,
and only one is imported per session.
Currently-imported Qt library: 'pyqtv1'
PyQt4 installed: True
PySide >= 1.0.3 installed: False
Tried to load: ['pyside', 'pyqt']
I've traced this error to IPython's qt module, but essentially forcing 'pyqtv1' to be loaded (by replacing api_opts = [QT_API] with api_opts = ['pyqtv1'] breaks QString inside the IPython window.
However, if I make it so that those imports happen first (by importing the module this code is in before anything else), that error goes away and it basically breaks QString completely with an ImportError: cannot import name QString.
I've verified that if I make a self-contained QApplication in the module that contains the IPython instance, making sure to import PyQt.QtGui after doing the IPython imports, it works ask expected. It's only when I try and mix the IPython code with any other PyQt code that things break.
Any suggestions as to how to fix the error?
I'm on Ubuntu Linux 12.04, and IPython is version 2.0.0-dev pulled on March 10th.

PyQt offers two different APIs for strings: you can choose which one to use with code like this:
import sip
sip.setapi('QString', 2)
from PyQt4 import QtGui
Once you import PyQt4, that API is set and cannot be changed. On Python 2, it defaults to version 1 for backwards compatibility, but IPython requires API version 2. Version 2 is the default on Python 3, and PySide's API is equivalent to version 2.
If you've used the v1 API in your application, you cannot use the IPython Qt console embedded in that application, except by porting it to the v2 API. However, you can embed an IPython kernel in your application, and connect to it from a Qt console in a separate process.

Related

ModuleNotFoundError: No module named 'QWebEngineView'

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.

matplotlib does not import PyQt4, PyQt5 or PySide

I'm using Canopy 2.1.3 distribution (on Ubuntu 16.04) which as you may know contains the matplotlib package. Simply running in the Canopy's shell import matplotlib.pyplot as plt gives me
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I have installed all of the three packages with Synaptic but the problem persists.
Could this be related to environment variables? Or, could it be because Canopy's Python 3.5 and Ubuntu's Python 2.7 are conflicting?
EDIT:
I have finally used the PyQt by installing it from the Canopy's package manager.
It is likely that the default backend for matplotlib.pyplot is set to qt.
I have very little experience with canopy, but it should be possible to change the default to another back end; maybe someone else will be able to tell you how, or you can find it by yourself.
In the meantime, you can try to add this above all other matplotlib imports:
import matplotlib
matplotlib.use('TkAgg')
From the user guide:
Known Issue: In Canopy running Python 3, GUI backend must be explicitly set
If you ask matplotlib to display a plot, or otherwise run Python code that creates a GUI window, by default you will get an error traceback ending with: ImportError: No module named 'PyQt4'
The solutions are to install PyQt (which is GPL-licensed) in the Canopy Package Manager, or to use the Canopy Preference menu’s Python tab to specify the TK PyLab backend for creating GUIs. For details, see “Python 3 in Canopy 2 - ‘No module named PyQt4’ error”

wxPython version issue

Running Ubuntu 14.04 with Eclipse Kepler Service Release 2 which has Pydev 4.4.0.2... and Pydev Mylyn Integration 0.6.0. My program runs with no issues. Porting it over to my new platform running Ubuntu 16.04 with Eclipse Oxygen 1A release 4.7.1A and Pydev 6.0.0... with Pydev Integration at 0.6.0 and get the following error:
from wxPython._gdi import wxBitmapFromImage
ImportError: No module named wxPython._gdi
I can import wx either in Eclipse (Console) or on a terminal shell in the new ubuntu version but get the 2nd line error when I type the 1st line. My 14.04 is running wxpython 2.8 while my new 16.04 is running 3.0 not sure if this is it however.
I had the same issue except I got it on unpiclikg an instane of _gdi.Colour class. Following advice from Unpickling python objects with a changed module path I was saved with:
import wx as _wx
from wx import _core
sys.modules['wx._gdi'] = _core
This is a hack of course and you should be better off with a try-except as in (untested)
try:
from wxPython._gdi import wxBitmapFromImage
except ImportError:
from wx import BitmapFromImage as wxBitmapFromImage
The wxPython namespace is very, very old. As in more than a decade. If I remember correctly it was deprecated in 2.7 and provided only as a thin compatibility shim in 2.8, with the intent that all should have migrated to the new wx namespace by then. The compatibility shim was removed in 2.9.
The gist of the changes is that names like wxFrame located in the wxPython namespace or one of its submodules have been changed to names like Frame in the wx namespace. In other words, it should be used like this: wx.Frame.

Python: cannot import from PyQt5.QtCore (attempting example script)

I'm introducing myself to PyQt5 through one of its included examples. On this windows 7 machine, I have installed Python 3.4 x64, and PyQt5 using its binary provided on riverbankcomputiong.com. The documentation says that the binary already includes everything necessary to run. I (perhaps incorrectly) assumed that i can safely skip the "configure" and "build" steps at Riverbank's installation guide, since the guide only talks about .zip, .tar, etc. files.
I used the tutorial located here:
http://www.pythonschool.net/pyqt/introduction-to-pyqt/
Which also says "just run the binary to install pyqt4 there is no step three."
When i attempt to run any tutorial containing reference to PyQt4 or PyQt5:
from PyQt5.QtCore import *
I get the following error message:
ImportError: DLL load failed: The specified module could not be found.
But when i enter the following:
import PyQt5
The interpreter seems to be okay with it -- no errors.
I can't help but think I've done something wrong installation, because even when I run the examples included with PyQt4/PyQt5, i get importerrors. It seems as though QtCore doesn't even exist in relation to PyQt4 or PyQt5. What's going on here?
There seem to be a few possibilities:
You might need to update your PATH environment variable to include the location where the library is installed (i.e. directory location where the DLLs are)
Take note of where you're launching the interpreter or scripts from as their current path may not necessarily include what you expect.
You can check this with:
>>> import os
>>> os.environ['PATH'].split(os.pathsep)
Make sure the installation process was truly successful (i.e. zero errors) and that you downloaded the correct binary package for your PC's architecture.
I ran into a similar issue with PySide where import PySide would work but import PySide.QtCore would fail.
I'd expect the code to work after verifying those things:
>>> from PyQt5.QtCore import *
>>>
If it doesn't, update the question with additional details like things you checked, their values, error messages, etc.

Running matplotlib with PyQt on Windows

I'm having trouble running matplotlib with PyQt on Windows 7.
When executing any code that uses these two libraries, even demo code such as: http://eli.thegreenplace.net/files/prog_code/qt_mpl_bars.py.txt
I get this error:
TypeError: 'PySide.QtGui.QWidget.setParent' called with wrong argument types:
PySide.QtGui.QWidget.setParent(QWidget)
Supported signatures:
PySide.QtGui.QWidget.setParent(PySide.QtGui.QWidget)
PySide.QtGui.QWidget.setParent(PySide.QtGui.QWidget, PySide.QtCore.Qt.WindowFl
ags)
I'm using Python 2.7.5, matplotlib 1.2.1, and PyQt 4.10.3. All of these are 32-bit, running on my 64-bit OS. I've ran code which used matplotlib and PyQt separately with no issues.
I've tried the solution suggested in Getting PySide to work with matplotlib with no success.
Please help me track down this error.
I am not clear if you want to use PySide or PyQt, but in either case the problem is you want to use one, but matplotlib is setting up the Qt4 backend using the other one, hence the confusing looking errors.
If you want to use PySide make sure
backend.qt4 : PySide # PyQt4 | PySide
is in your matplotlibrc file.
If you want to use PyQt make sure
backend.qt4 : PyQt4 # PyQt4 | PySide
is in your matplotlibrc file.

Categories

Resources