Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install PyQt5 on a new virtualenv and work on an IDLE

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.)

like image 325
Gouz Avatar asked Aug 04 '13 12:08

Gouz


People also ask

How do I set the Python version for a virtual environment?

By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag : $ virtualenv -p python3. 2 my_env Running virtualenv with interpreter /usr/local/bin/python3.

How do I launch PyQt?

Go to C:\Program Files (x86)\Python36-32\Lib\site-packages\pyqt5-tools and locate designer.exe . Double click to open the Qt Designer. Note: The path will vary based on the OS you're using. The Qt Designer will provide some basic templates.

What is PIP install PyQt5?

Project description PyQt5 is a comprehensive set of Python bindings for Qt v5. It is implemented as more than 35 extension modules and enables Python to be used as an alternative application development language to C++ on all supported platforms including iOS and Android.

Which Python version supports PyQt5?

PyQt5 - What's New PyQt5 is not supported on versions of Python earlier than v2. 6. Classes defined in earlier QtGui module have been distributed in QtGui, QtPrintSupport an QtWidgets modules.


3 Answers

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.

like image 126
boweeb Avatar answered Oct 26 '22 18:10

boweeb


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'.

like image 30
Six Avatar answered Oct 26 '22 19:10

Six


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")])
like image 3
Anon Avatar answered Oct 26 '22 18:10

Anon