We can easily fix the error importerror cannot import name qtwidgets by just installing pyqt5 python package using pip , conda etc. Actually, qtwidgets is package/module of pyqt5 . Hence once pyqt5 is successfully installed on the system, You will not get the same error. In this article, We will explore those ways to install the pyqt5 python package.
Importerror cannot import name qtwidgets : ( Solution )
The first and most convenient way is the pip package manager. So let’s go with it.
Here is the command for installing pyqt5 python package.
pip install PyQt5
Remember the above command will install the latest version for PyQt5 package. If you want to go with some other compatible version, You may have to define it with the below command.
pip install PyQt5==5.15.3
Here you may pass any other version for PyQt5 as per your compatibility. Last but not least to add – sudo prefix. In some places where the directory needs admin privilege’s to run. You may add sudo prefix before above mention command.
Method 2 :
Let’s use conda for installing PyQt5 package. Here is the command for this-
conda install -c ipa pyqt5
Conda is the default package manager from Anaconda.
How to check the error is fix?
The simple way to check the above error “cannot import name qtwidgets” is either you run the same code where you were getting the same error. Another way is to run the import line if it is not showing any error. We have simply fixed that error. As you already know QtWidgets is the submodule from PyQt5 package.
import PyQt5.QtWidgets as QtWidgets
Hope you will be able to troubleshoot the error. Still, if you are facing the same issue, Let the team know about the same. We will surely help you to fix the same as soon as possible.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.