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-package
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.
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.
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")])