I have been attempting to learn PyQt5 in order to create GUI.
Pip installed the PyQt5 & the PyQt5.tools yet when I attempt to use it
using Visual Studio Code (while having the Python Extension of VS Code installed) I receive an error.
VS Code detects PyQt5 up to PyQt5.QtWidgets, yet it says no module inside QtWidgets named QApplication can be found.
Import Error of VS Code
On the contrary when it comes to the error, when I attempt to import QApplication from python.exe, no errors nor problems are presented.
Python.exe 'from PyQt5.QtWidgets import QApplication'
This leads me into thinking the problem resides at VS Code's doorstep..
Thanks for your time/answers.
open the cmd from start menu (Command Prompt)
type pip install pyqt5
type pip install pyqt5-stubs
install the integration of PyQt5 from VSCode Marketplace
if is it still not working try uninstalling PyQt5 (pip uninstall pyqt5)
and install it again
Search "PYQT Integration" on ur VS Code Extension shop
Install it
Set the configurations (ur pyqtdesigner path and pyuic etc)
this may help u
I had the same problem with lots of modules specially pytube . PyQt6 , It was all resolved when I changed my interpreter to python 3.11
Related
I donwloaded PyQt6 using pip install pyqt6 and it had a bunch of errors so I uninstalled it and reinstalled it with pip install pyqt6 --user and the errors dissappeared. problem is trying to use it in VSCODe or any other ide doesn't work. When i write:
from PyQt6.QtWidgets import QApplication, QMainWindow, QAction, QMenu, QMessageBox VSCode gives me the error Import "PyQt6.QtWidgets" could not be resolved. Could someone walk me through what I may of done wrong please?
Try this in VSCode:
On the lower right of the VSCode window you should see the language
mode and version that VSCode is currently using. Something like "Python" 3.10.7.
If you HOVER over this text (like 3.10.7), VSCODE will display the
actual path used for that version.
If you CLICK on the version, you can change the LANGUAGE MODE and VERSION that is being used by VSCode in the window while editing (and executing) Python code. The language and version reflect the path to the library used.
In my case when I looked, the version had changed to a beta version I'd mistakingly installed earlier (3.11.03b). The version I was using when I installed PyQT6 was 3.10.7.
When I changed the version back to 3.10.7, suddenly everything worked again.
In my case, I had been moving things around in migrating from PyQT5 to PyQT6 and was now using a different file folder for my migrated code. I opened up a new VSCode Window and then my new PyQT6 folder, and I failed to notice that VSCode had chosen what it thought was the 'latest' version of the interpreter.
Im working on application design using Qt Design. the design completed so now i want to connect it with Python 3.6.4 using pycharm. and import PyQt5. i already installed PyQt5 as you can see.
PS C:\Users\Tariq> pip install pyqt5
Requirement already satisfied :pyqt5 in
c:\users\tariq\appdata\local\programs\python\python36-32\lib\site-packages
Requirement already satisfied: sip<4.20,>=4.19.4 in
c:\users\tariq\appdata\local\programs\python\python36-32\lib\site-packages
(from pyqt5)
also i installed PyQt5 inside pycharm as you can see in the picture.
Press enter to show the image that PyQt5 installed inside pycharm
My Problem that the PyQt5 unknown for pycharm and this error appear
from PyQt5.QtWidgets import*
ModuleNotFoundError: No module named 'PyQt5
'
The way that has always worked for me is below.
from PyQt5 import QtCore, QtGui, QtWidgets
This maybe due to the way you set up your environment variable add this c:\Python3*\Script,C:\Python3*\Lib\site-packages\, and C:\Python34\Lib\site-packages\PyQt5\ in your environmental path, hope it works. '*` is the version of python e.g Python34
I have built a GUI using PyQt5 and wanted to distribute it to other computers without Python installed by packageing it with pynsist. Unfortunately, when launching the shortcuts produced by the installer, the GUI does not open as expected and Python keeps crashing (even if Python is instlalled).
The console shows the following error upon crashing:
ModuleNotFoundError: No module named 'sip'.
This is due to from PyQt5.QtCore import *.
Unfortunately, when launching the .launch.py script on a computer with Python installed, everything works fine. I suppose this is because it then targets the installed version instead of the version included in the build of the GUI.
However, the shortcuts do not work, no matter if Python is installed, giving me the above error.
Any ideas how to fix this problem and make the shortcuts work on computers that do not have Python installed?
Thank you in advance!
When I first encountered this problem I simply added sip.pyd to my project-folder. You can find it under:
x:/path-to-python/Lib/site-packages/sip.pyd
but what realy solved my problem was using pyinstaller.
It automatically detects most dependencies and most of the time runs without complicated configurations.
pip install pyinstaller
pyinstaller main.py
# done
Sip is a separate package which PyQt5 requires. You can include it with your application by listing it in pypi_wheels=, as in the PyQt5 example:
[Include]
packages=listapp
pypi_wheels= PyQt5==5.6
sip==4.18
If you're using the latest version of PyQt5 (5.10.1), then the latest version of sip (4.19.8) should work with it.
On an OSX 10.9, I have Qt5 installed. Later I installed Ipython, sip and PyQt5 all build from source. Now here is the problem: when I try to run ipython qtconsole, I have a bunch of errors related to the files in this directory (and its subfolders)
/Library/Python/2.7/site-packages/IPython/
followed by
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: None
PyQt4 installed: False
PySide >= 1.0.3 installed: False
Tried to load: ['pyside', 'pyqt']
I assume the problem is that I have PyQt5 instead of PyQt4. Is this the problem or I have missed some thing else?
Is there anyway that I can have qtconsole with PyQt5? If no, what is the easiest way to do so? (preferably, a method that does not involve having two versions of Qt library on one machine).
For sake of brevity I skipped posting the full error message. Please let me know if I should add them from better understanding of the situation.
The problem might be related to your python path.
I had almost the exact same problem. I had installed PyQt using Homebrew and I was getting the same error message. Finally what solved the problem was adding the following line to my .bash_profile:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
Since I am a beginner myself, I can't help you any further but I hope this solves the problem.
I know this is really old, but I was recently having trouble setting up PyQt5.
The problem was that PyQt5 would install, but some of the sub modules would not. For instance try
import PyQt5 #works
from PyQt5 import * # might fail
If the second command fails, then you have a problem with the PyQt5 setup, and you need to focus on that.
Specifically, ipython+qt will try to import the following
from PyQt5 import QtCore, QtSvg, QtWidgets, QtGui
If any of these sub-modules are broken/missing the qt console launch will quietly fail.
For me it turned out that the PyQt5 installer was failing to make the QtSvg bindings, which ipython tries to load from PyQt5. When you run the configure.py, use the -w option and you will see all the details of the build, allowing you to pinpoint where the install is failing.
In my case, a little googling and I found out that I was missing the QtSvg lib, which can be pulled in easily from the repos.
Setting environment variable QT_API=pyqt5 solved the same problem for me.
I had the same problem, though with the most recent brew of ipython the ImportError included PyQt5 (along with PyQt4 and PySide). So if it didn't work with PyQt5 before, it does now.
Adding the correct PYTHONPATH to .bash_profile fixed it for me, even in my virtualenv. (I don't have the reputation to up-vote or comment on oxtay's answer, where this would be more appropriate...)
I installed PyQt5 globally on my win7 system (python 3.3), using the installer provided from the official riverbank website.
Then i created a new –no-site-packages virtualenv, where the only things i see listed after typing pip list, are pip (1.4) and setuptools (0.9.7).
The problem now however, is that i need to install there the complete PyQt5 too and this seems impossible using the pip tool.
Both pip install sip and pip install PyQt5 inside the virtual enviroment are returning errors.
Can someone provide a "how to" guide of what exactly should i do?
I also want to be able to work with that PyQt5 (of the new virtualenv) from inside an IDLE, so
I copied the tcl folder from the global installation of my Python to the location of my virtual environment and also created a shortcut targeting: {location of my virtual enviroment}\Scripts\pythonw.exe C:\Python33\Lib\idlelib\idle.pyw so i could open the virtualenv IDLE and not the global one. (Hope I did not do anything wrong there... correction maybe please.)
Both "pip install sip" and "pip install PyQt5" inside the virtual enviroment are returning errors.
If the errors you're referring to are:
Could not find any downloads that satisfy the requirement [pyqt5|sip]
and
No distributions at all found for [pyqt5|sip]
Then this answer should shed light on this. Basically, PyPI is only providing a link to the homepage and/or source -- not a downloadable package or egg. Here are the links to PyQt5 and SIP PyPI pages. I know it seems strange that a package manager wouldn't provide packages, but that's the way it is.
You'll have to download, compile, and install sip and pyqt5 from source, inside your virtualenv. I wish I could provide details but I'm currently working through this myself.
Assuming you have PyQt5 installed globally, there's a way you can give your virtualenv access to it without using --system-site-packages. A package called vext makes it possible. It works with other packages as well, but in this case we'll just be setting up the PyQt5 support.
Run the following command from within your virtualenv and you should be good to go:
pip install vext.pyqt5
To check the status, run vext -l. You should see something like the following:
Running in virtualenv [enabled]
pyqt5.vext
Or, run vext -c pyqt5.vext. You should see something like the following:
import sip: [success]
import PyQt5: [success]
Alternatively you can just try to import PyQt5, e.g. python -c 'import PyQt5'.
How I got my PyQt5, Python33 Windows7x64 within a virtualenv working:
Firstly, ensure you have matched 64 or 32 bit versions of everything. I am developing on a 64bit platform and am using 64bit versions. I did not fully test this with 32 bit. Please read thoroughly.
Installed Python33 x64 using Windows installer binaries.
Installed PyQt5 x64 from riverbank using the MSI installer into the default Python33. PIP won't work.
Create a virtualenv using the Python33 as your base, no site packages.
Copy the PyQt5 folder from Python33/Lib/site-packages/ into your virtualenv/Lib/site-packages.
DO NOT DELETE THE PyQT5 folder!
Ensure PyQt5 is working on the base Python33 install:
from PyQt5 import QtCore, QtGui, QtWidgets
Create a main window and launch it.
If testing with PyQt4 code, a few classes have been swapped around. (Within QtGui and QtWidgets for example)
from PyQt5 import QtGui, QtWidgets, QtCore
class Main(QtWidgets.QMainWindow):
def __init__(self):
print("Main __init__ fired")
QtWidgets.QMainWindow.__init__(self)
#Setup the UI
print("Setting up UI")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def main():
print("fired main()")
#Setup the main application object
app = QtWidgets.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
#Run GUI
if __name__ == "__main__":
main()
If you get this error "..failed to start, could not find or load the Qt platform plugin "windows." (or similar), YOU MUST UPDATE your DirectX. Do this through Microsoft.
Once you are entirely sure PyQt5 is working within Windows, then continue.
Activate your virtualenv where you copied the PyQt5 folder to
Ensure PyQt5 can be imported from within your virtualenv. You may need to manipulate paths depending on what IDE you are using. Run an import in your virtualenv
from PyQt5 import QtCore, QtGui, QtWidgets
Try to run the same application again from inside this virtualenv
OH NO! Another error with dlls! What gives? The paths to the QT dll libraries are wrong inside the virtualenv. It happens even if you tell PyQt5 to install directly to your virtualenv. You MUST add the paths to libraryPaths before creating the QApplication object. Here is a solution (Replace venv with your virtualenv path):
def main():
print("fired main()")
#ADD THE FOLLOWING BEFORE CREATING A QApplication()
QtCore.QCoreApplication.setLibraryPaths(['C:/venv/Lib/site-packages/PyQt5/plugins'])
#Ensure path was added and correct
print(QtCore.QCoreApplication.libraryPaths())
#Setup the main application object
app = QtWidgets.QApplication(sys.argv)
#See what the paths were
print(app.libraryPaths())
window = Main()
window.show()
sys.exit(app.exec_())
Now the code should run from all inside your virtualenv. You can delete the PyQt5 directory from your base install, or move it to another place if you need it in the future.
There is most likely a way to boilerplate the path in a relative way, but this could create an issue with cx-freeze. Speaking of which.
Now you're all done and you go to cx-freeze your PyQt5 app into a nice exe. You go to run it and OH NO! more dll troubles.
You can manually copy libEGL.dll from site-packages/PyQt5 to the root build folder of your application, or add the following lines to your setup.py file for cx-freeze:
import sys
from cx_Freeze import setup, Executable
#ADD THIS MUST INCLUDE FOR Windows 7.
include_files = [('c:/venv/Lib/site-packages/PyQt5/libEGL.dll', 'libEGL.dll')]
setup(
name="My App",
version="0.1",
description="PyQt5 App.",
#ADD THIS
options={'build_exe': {'include_files': include_files}},
executables=[Executable("c:/venv/src/myApp_main.py",
base="Win32GUI",
targetName="MyApp.exe")])
I faced the same issue installing PyQt5 and SIP. The solution for resolving this problem for PyQt4 as specified on Is it possible to add PyQt4/PySide packages on a Virtualenv sandbox? worked for me.
I manually created symlinks to the PyQt5 and sip.so from within the virtualenv/site-packages directory. An ugly solution but gets things working.
Anon's solution of adding a Qt libraryPath worked for me. I am using Anaconda3 on Windows. But I found an alternative.
Copy the file …\Anaconda3\qt.conf to the Scripts folder in the virtual environment. Now I don't need to change any Python code.
The conf file seems to have been created by …\Anaconda3\Scripts\.qt-post-link.bat.