I'm trying to run an example script using PyQt 5 that contains the following:
import sys
from PyQt5 import QtCore, QtGui, uic
This throws:
ImportError: cannot import name 'QtCore'
I'm running 64-bit versions of both Python 3.4 and PyQt 5 on 64-bit Windows 10. When I try
from PyQt5 import QtCore
in the terminal it works fine; I only get the import error when running a script. Other similar questions on StackOverflow led me to check sys.path, which produced:
>>> print(sys.path)
['', 'C:\\Windows\\SYSTEM32\\python34.zip', 'C:\\Python34\\DLLs',
'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages',
'C:\\Python34\\lib\\site-packages\\PyQt5']
os.environ['PATH'].split(os.pathsep)
results in a long list of directories, including the location of PyQt5 in the Python34 folder.
I've also tried uninstalling and reinstalling both Python and PyQt, and tried PyQt4 instead of 5, all with no success.
Related
I am trying to import PyQt5 by :
from PyQt5 import QtCore, QtGui, QtWidgets
But it is giving me the Error : Unable to import PyQt5. PyQt5 is definitely installed in the Virtualenv in the location -
"c:\users\TM\documents\python projects\pyqt test\env\lib\site-packages"
Which I did by - 'py -m pip install pyqt5' while in the (env).
My program is also in the 'pyqt test' folder.
I've installed Python on a new PC and this is the first time I'm having this problem, before I could just import anything in an env without any error.
EDIT: This is my sys.path:
['c:\\Users\\TM\\Documents\\Python Projects\\PyQt Test',
'C:\\Users\\TM\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
'C:\\Users\\TM\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
'C:\\Users\\TM\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
'C:\\Users\\TM\\AppData\\Local\\Programs\\Python\\Python38-32',
'C:\\Users\\TM\0\AppData\\Roaming\\Python\\Python38\\site-packages',
'C:\\Users\\TM\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
I've recently upgraded PyQt5 from 5.5.1 to 5.6.0 using the Windows 32-bit installer here: https://www.riverbankcomputing.com/software/pyqt/download5. I've also upgraded my python from 3.4 to 3.5.
When I run my old code (which used to work) with the latest version I get an exception:
from PyQt5.QtWebKitWidgets import *
ImportError: No module named 'PyQt5.QtWebKitWidgets'
All of my QT calls in my python occur consecutively and are (and I know I shouldn't be importing * but that's beside the issue here I think):
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
So the QtCore, QtGui and QtWidgets imports are all OK.
Also, when I search the source for QtWebKitWidgets there appears several references to this module.
Finally my python path looks like:
C:\PYTHON35;C:\PYTHON35\DLLs;C:\PYTHON35\LIB;C:\PYTHON35\LIB\LIB-TK;
and environment path:
C:\Python35\Lib\site-packages\PyQt5;C:\Python35;C:\Python35\Lib;C:\Python35\Lib\site-packages;C:\Python35\Scripts ....
QtWebKit got deprecated upstream in Qt 5.5 and removed in 5.6.
You may want to switch to PyQt5.QtWebEngineWidgets.QWebEngineView.
For basic use of PyQt5.QtWebKitWidgets.QWebView, it can simply be updated to use PyQt5.QtWebEngineWidgets.QWebEngineView in the source code, but there may be some differences in the new component which require further adjustments.
I was trying to run qutebrowser and it had the same error, the answer is simple, the packages changed.
You have two solutions:
1)
pip install PyQtWebEngine
2)
pip install PyQt5==5.11.3
Hope this helps any future problems
In PyQt5 "QtWebKitWidgets" is Deprecated. I just replace this line
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtWebKit import QWebSettings
With this code:
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView,QWebEnginePage as QWebPage
from PyQt5.QtWebEngineWidgets import QWebEngineSettings as QWebSettings
If you really want to use PyQt5.QtWebKitWidgets, you could run this from the command line:
pip install PyQtWebKit
and let it do what it does.
In PyQt5 "QtWebKitWidgets" is no longer available. Instead it is replaced with "QtWebEngineWidgets". So you have to make this change in your code.
For more information: http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
OVERVIEW
I'm having troubles importing modules after the line
QtWidgets.QApplication(sys.argv), let's say I got this little snippet main.py:
import sys
import importlib
from PyQt5 import QtWidgets
print('Sys Path:')
print(' %s\n' % '\n '.join(sys.path))
if sys.argv[-1] == '1':
print('Importing Before...\n')
from PyQt5 import Qt
app = QtWidgets.QApplication(sys.argv)
elif sys.argv[-1] == '2':
print('Importing After...\n')
app = QtWidgets.QApplication(sys.argv)
from PyQt5 import Qt
print('Done')
If I run python main.py 1 everything works as expected.
If I run python main.py 2 the process hangs (probably in a infinite loop) without giving any error.
Output of python main.py 2:
(py352) D:\sources\personal\python\pyqt\mcve>python main.py 2
Sys Path:
D:\sources\personal\python\pyqt\mcve
D:\sources\personal\python
d:\virtual_envs\py352\Scripts\python35.zip
d:\virtual_envs\py352\DLLs
d:\virtual_envs\py352\lib
d:\virtual_envs\py352\Scripts
c:\Python352\Lib
c:\Python352\DLLs
d:\virtual_envs\py352
d:\virtual_envs\py352\lib\site-packages
Importing After...
(HANG)
ATTEMPTS
Tested with a couple of virtualenvs on win7:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 on win7
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Pyqt was installed on the virtualenvs using pip and the versions are these ones:
>>> QtCore.QT_VERSION
329472
>>> QtCore.QT_VERSION_STR
'5.7.0'
>>> QtCore.PYQT_VERSION_STR
'5.7'
RELEVANT INFORMATION
Some very nice people from #pyqt freenode channel helped me to test the repo and none of them were able to reproduce the issue, the python versions and platforms they used were:
win10 - 3.5.2 |Anaconda 4.1.1 (64-bit)
win8 - 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18)
ubuntu 16.04 - 3.5.2 (default, Nov 17 2016, 17:05:23)
QUESTIONS
What's the reason of from PyQt5 import Qt (or others pyqt modules) getting stuck on my box and other people not being able to reproduce?
How can I fix this behaviour? This is important to me cos I'd like to load plugins dynamically once my pyqt applications has started
This is not (yet) a real answer, but it might provide the first step in how to find one.
Below is the minimal test case I suggested in my first comment on the question. It tests just one thing: does calling importlib.import_module after creating a QApplication make the interpreter hang on your system? Note that, for now, it only tries to import a module from the python standard library. It is vital to proceed one step at a time, and be careful to avoid introducing any potentially confounding variables.
Please run this script exactly as described below, and add the output to your question. (Even if option 2 doesn't hang, the sys.path details are likely to be relevant).
import sys, importlib
from PyQt5 import QtWidgets
print('Sys Path:')
print(' %s\n' % '\n '.join(sys.path))
mod = None
modname = 'collections.abc'
# modname = 'PyQt5.Qt'
if sys.argv[-1] == '1':
print('Importing Before...\n')
mod = importlib.import_module(modname)
app = QtWidgets.QApplication(sys.argv)
elif sys.argv[-1] == '2':
print('Importing After...\n')
app = QtWidgets.QApplication(sys.argv)
mod = importlib.import_module(modname)
# from PyQt5 import Qt
print('Result: %r' % mod)
Run the script like this:
$ python /tmp/test.py 1
then like this:
$ python /tmp/test.py 2
On my system (ArchLinux, Python-3.5.2, Qt-5.7.1, PyQt-5.7), the second one produces the following output:
Sys Path:
/tmp
/usr/lib/python35.zip
/usr/lib/python3.5
/usr/lib/python3.5/plat-linux
/usr/lib/python3.5/lib-dynload
/usr/lib/python3.5/site-packages
Importing After...
Result: <module 'collections.abc' from '/usr/lib/python3.5/collections/abc.py'>
UPDATE:
The first step established that importlib is not itself the cause of the problem. The second step is to identify which specific imported module is the source of the problem.
I have added two (commented) lines to the test script that will allow this to be done. The first checks whether importing PyQt5.Qt triggers a hang. If it does, the second checks whether a normal import statement will also trigger the hang.
Note that from PyQt5 import Qt effectively imports everything, including some very heavyweight and potentially troublesome modules like QtWebEngineWidgets. So it will be necessary to further refine the imports to properly identify the precise source of the problem.
UPDATE 2:
The QtWebEngineWidgets is a well-known source of problems, and often needs careful handling. The following interpreter session output seems relevant to your current problem:
>>> from PyQt5 import QtWidgets, QtCore
>>> app = QtWidgets.QApplication([''])
>>> from PyQt5 import QtWebEngineWidgets
Qt WebEngine seems to be initialized from a plugin. Please set Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing QGuiApplication.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created
>>>
>>> QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
>>> from PyQt5 import QtWebEngineWidgets
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created
This is all normal behaviour (although at the moment I can't find any official documentation for it). But let's try the same thing using the Qt module:
>>> from PyQt5 import QtWidgets, QtCore
>>> QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
>>> app = QtWidgets.QApplication([''])
>>> from PyQt5 import Qt
>>> Qt.QWeb
Qt.QWebChannel( Qt.QWebEngineUrlRequestInterceptor( Qt.QWebHitTestResult( Qt.QWebSocketCorsAuthenticator(
Qt.QWebChannelAbstractTransport( Qt.QWebEngineUrlRequestJob( Qt.QWebInspector( Qt.QWebSocketProtocol(
Qt.QWebDatabase( Qt.QWebEngineUrlSchemeHandler( Qt.QWebPage( Qt.QWebSocketServer(
Qt.QWebElement( Qt.QWebFrame( Qt.QWebPluginFactory( Qt.QWebView(
Qt.QWebElementCollection( Qt.QWebHistory( Qt.QWebSecurityOrigin(
Qt.QWebEngineCookieStore( Qt.QWebHistoryInterface( Qt.QWebSettings(
Qt.QWebEngineUrlRequestInfo( Qt.QWebHistoryItem( Qt.QWebSocket(
>
So it looks like there's special handling for the Qt module when it's imported after a QApplication is created - although some QWebEngine classes are available, most of them have been omitted (e.g. QWebEngineView, QWebEnginePage, etc). But it seems that in your particular setup, this may not be working as it should. If so, you'd probably have to take this up with the author of PyQt, as it might require knowledge of the inner workings of the Qt module.
Using these lines in your code after importing libraries will help.
from PyQt5 import QtWidgets
app = QtWidgets.QApplication.instance()
if app is not None:
import sip
app.quit()
sip.delete(app)
import sys
from PyQt5 import QtCore, QtWebEngineWidgets
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
app = QtWidgets.qApp = QtWidgets.QApplication(sys.argv)
I've successfully installed Python 3.5.1 and PyQt5. But as soon as I try to import anything from PyQt5, it fails like this:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
File "stdn", line 1, in "module"
ImportError: Dll load failed: The specified module could not be found.
I've checked my PATH and everything is OK: I have both D:\Python3\Lib\site-packages\PyQt5 and D:\Python3\Lib\site-packages.
Python and PyQt5 are both 64-bit versions.
Path Printscreen:
This is a printscreen of my Environment variable Path
I have found plenty of posts regarding this issue, but no answers that work for me.
PROBLEM:
I am trying to run this code:
from PyQt4 import QtCore, QtGui
I get this error in return:
ImportError: cannot import name QtCore
I append the path to PyQt4 to sys.path (C:\Python27\Lib\site-packages\PyQt4) and importing just PyQt4 throws no errors. I can see that QtCore.pyd and QtGui.pyd are in that directory and the directory has its __init__.py file.
Specs:
Windows 7 x64
python v2.6.8
PyQt v4.10.4
If you're using Python-2.6.x, you will have to use an installer for an earlier version of PyQt4. The most recent version available is for PyQt-4.10/Qt-4.8.4, which can be downloaded from here:
PyQt4-4.10-gpl-Py2.6-Qt4.8.4-x64.exe
PyQt4-4.10-gpl-Py2.6-Qt4.8.4-x32.exe