python-qt5 icon indicating copy to clipboard operation
python-qt5 copied to clipboard

Qt platform plugin "windows"

Open mottosso opened this issue 11 years ago • 35 comments

Cannot instantiate a QApplication.

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

Results in:

This application failed to start because it could not find or load the Qt platfo
rm plugin "windows".

Available platform plugins are: minimal (from C:\Python27\lib\site-packages\PyQt
5\plugins\platforms), offscreen (from C:\Python27\lib\site-packages\PyQt5\plugin
s\platforms), windows (from C:\Python27\lib\site-packages\PyQt5\plugins\platform
s).

Reinstalling the application may fix this problem.

Having encountered the problem in the past, I could solve it by appending the plugins by hand like this:

import os
import sys

import PyQt5

dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)

However this produces the same error. The odd thing is that it says it can't load the plugin, and then lists the plugin as one of the available ones. Maybe a permission issue? Yet permissions on the files are unlocked and even replacing it with the source original from the Qt installation yields the same results. Removing the plugin from the directory correctly removes it from plugins found.

depends.exe

Running depends on qwindows.dll yields a few interesting results.

depends

However each of these are available directly within the PyQt5 Python package directory.

It works

If keeping the original installation of Qt on the PATH, PyQt5 seems to work. From a terminal:

# Keeping C:\Qt\Qt5.3.0\5.3\msvc2013_64\bin on the PATH

import os
import sys

from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
button = QtWidgets.QPushButton("Hello World")
button.show()

So there must be something within this directory which is requited by PyQt5. But copying the entire contents into the PyQt5 Python library directory yields no better results.

mottosso avatar Sep 18 '14 07:09 mottosso

I've had the same issue with cx_Freeze - there for me it helped to copy libEGL.dll to the frozen package.

Maybe that helps in some way?

The-Compiler avatar Sep 18 '14 08:09 The-Compiler

Thanks @The-Compiler, I'll have a look at that tonight. You're thinking of just putting it in the root PyQt5 Python library directory?

mottosso avatar Sep 18 '14 08:09 mottosso

I'm afraid I have no idea what to do with it (and I don't know anything about putting C projects on PyPI at all), but that's where I remember the message from (with windows being listed in the available platforms as well).

The-Compiler avatar Sep 18 '14 08:09 The-Compiler

I'm afraid I have no idea what to do with it

No problem, it does help to narrow down the dependencies and this seems to be one of them.

(and I don't know anything about putting C projects on PyPI at all

Though I'm sure there are practices for doing this more gracefully, the way I'll handle it initially is the same as with any Python package with package data; to simply include all non-Python files in the package_data argument of setup().

  • https://github.com/pyqt/pyqt5/blob/master/setup.py

mottosso avatar Sep 18 '14 09:09 mottosso

Seems to work now.

Here are the changes:

  • Compiled using Qt 5.3.1 rather than 5.3.2 as it isn't yet supported
  • Binaries was coming from the Python 3 binaries supplied via Riverbank's website but are now coming directly from the Qt distribution.
  • __init__.py now initialises PATH and QT_QPA_PLATFORM_PLUGIN_PATH

Platform plug-ins

The Riverbank distribution ships with a qt.conf which initialises the PATH and platform plugin path, this release instead initialises these via the __init__.py so as to not need to provide the qt.conf file.

Pre-requisities

To run this VS2013 compiled release of PyQt5, you'll need the VS2013 redistributables.

mottosso avatar Sep 29 '14 09:09 mottosso

This problem re-surfaces in certain situations that I am unable to re-produce.

image

The above was reported by @davidmartinezanim on a Windows 7 machine, running Python 2.7.9 x64 with Visual C++ 2013 re-distributable installed.

mottosso avatar Feb 05 '15 15:02 mottosso

I am having the same error with Python 2.7.9 x64 on Windows 8. I just freshly installed. Looking through the qt forums this is a problem for qt apps too. I have read that the app needs to have qt.conf and the platforms folder in its path. I'll check this later today or tomorrow.

emmatyping avatar May 23 '15 18:05 emmatyping

Well, I have a similar but slightly different problem. The windows platform option (or any option for that matter) doesn't show up at all. Interestingly, your above method of adding the os.environ value worked. The weird part is that setx QT_QPA_PLATFORM_PLUGIN_PATH "C:\Python27\lib\site-packages\PyQt 5\plugins\platforms" didn't work at all. I couldn't call the variable from cmd like PATH, and it didn't work with a Python script. Very weird.

emmatyping avatar May 24 '15 01:05 emmatyping

If you are installing from GitHub, and not PyPI, then you should have 5.4. If you do, have a go with this to see if it changes anything.

https://github.com/pyqt/python-qt5/wiki/Installation#install

It will create a qt.conf for you with the appropriate settings for your system.

To check the version of PyQt, try this.

>>> import PyQt5
>>> PyQt5.pyqt_version
'5.3.2'

mottosso avatar May 24 '15 10:05 mottosso

I installed from Github. I actually followed the steps you are pointing to, and I still got the error. Interesting. I just ran it again from the downloaded directory, and that might have fixed it. So maybe an extra step of running it again? Or just running it after install?

Well, I just re-installed from Github, and it worked if I ran the qt.conf generator after running python setup.py install. Very strange.

emmatyping avatar May 24 '15 15:05 emmatyping

It got weirder. I just got the message again. I tried generating a qt.conf again, but that didn't work. This is very annoying.

EDIT: and it is back. Perhaps re-running python -c "import util;util.createqtconf()"did work. I think I just needed to restart my ide. I am starting to think this is a valid solution.

emmatyping avatar May 30 '15 23:05 emmatyping

The behaviour is that Qt looks for qt.conf in the same directory as the current executable. Have a look at what executable your IDE is actually launching, it might be different from c:\Python27\python.exe. I've heard of this happening with PyCharm, for example, when running in debug versus release mode.

You can print the current executable like this.

import sys
print sys.executable

There should be a qt.conf in the same directory, and it should contain lines pointing to where your PyQt5 installation is at.

  • http://doc.qt.io/qt-5/qt-conf.html

It can get really complicated, so I don't blame you for getting frustrated. I've been there. :(

See the Bundling section of the wiki here for the full story.

  • https://github.com/pyqt/python-qt5/wiki/Updating-the-repository

mottosso avatar May 31 '15 11:05 mottosso

Okay, I am using PyCharm, but I am using Python27's python.exe (checked using your code). I think I have the problem. I was running python -c "import util;util.createqtconf()" from the downloaded directory, so all of the path information was pointing to Downloads\python-qt5, instead of the one in site-packages. I am not sure about the qt.conf. It currently is pointing to Lib\site-packages\PyQt5.

So having the install directory solved this issue, but raised another, it is trying to find my QtQml dll in my downloads (which doesn't exist).

EDIT: And none of the potential answers have done anything. I think I might need to poke around in my PATH and make sure things are pointing to the right places.

EDIT2: Still nothing. I have my PATH pointing towards the right folders, but I still have the problem.

emmatyping avatar May 31 '15 14:05 emmatyping

Just to clarify, this package does not require me to install Qt 5 as a dependency for this? It looks like python-qt5 includes all the necessary DLLs? Am I getting that right? Thanks!

jacksonofalltrades avatar Jan 18 '16 23:01 jacksonofalltrades

That's right!

mottosso avatar Jan 19 '16 06:01 mottosso

So I got this to work before, and now it's not working again :(

PyInstaller Bootloader 3.x LOADER: executable is Q:\exec\win\dist\main\main.exe LOADER: homepath is Q:\exec\win\dist\main LOADER: _MEIPASS2 is NULL LOADER: archivename is Q:\exec\win\dist\main\main.exe LOADER: No need to extract files to run; setting extractionpath to homepath LOADER: SetDllDirectory(Q:\exec\win\dist\main) LOADER: Already in the child - running user's code. LOADER: Python library: Q:\exec\win\dist\main\python27.dll LOADER: Loaded functions from Python library. LOADER: Manipulating environment (sys.path, sys.prefix) LOADER: sys.prefix is Q:\exec\win\dist\main LOADER: Setting runtime options LOADER: Initializing python LOADER: Overriding Python's sys.path LOADER: Post-init sys.path is Q:\exec\win\dist\main LOADER: Setting sys.argv LOADER: setting sys._MEIPASS LOADER: importing modules from CArchive LOADER: extracted struct LOADER: callfunction returned... LOADER: extracted pyimod01_os_path LOADER: callfunction returned... LOADER: extracted pyimod02_archive LOADER: callfunction returned... LOADER: extracted pyimod03_importers LOADER: callfunction returned... LOADER: Installing PYZ archive with Python modules. LOADER: PYZ archive: out00-PYZ.pyz LOADER: Running pyiboot01_bootstrap.py LOADER: Running pyi_rth_qt5.py Qt: Untested Windows version 10.0 detected! LOADER: Running pyi_rth_qt5plugins.py LOADER: Running pyi_rth_win32comgenpy.py LOADER: Running main.py RUNNING SMARTSHARE: Q:\exec\win\dist\main\PyQt5\plugins\platforms This application failed to start because it could not find or load the Qt platform plugin "windows".

Here is what I did:

  1. I used the git clone directly to get PyQt version 5.4.0
  2. I put this line at the very top of my script before Qt is used: import sys; import os; os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = os.path.join(sys._MEIPASS, 'PyQt5', 'plugins', 'platforms')
  3. I removed the qt.conf files from the pyinstaller dist folder
  4. I checked that the pyinstaller dist folder did indeed have PyQt5/plugins/platforms with a qwindows.dll in it.

I'm really desperate at this point. This worked on a Windows Server VM, but is not working on Windows 10 64-bit.

Any assistance would be greatly appreciated!

jacksonofalltrades avatar Jan 27 '16 22:01 jacksonofalltrades

Why step 3? I think you may need a qt.conf and it needs to reside next to the executable.

mottosso avatar Jan 27 '16 22:01 mottosso

Given what I said above, what paths should be in the qt.conf? That is, what are the paths in qt.conf relative to? Also, do the slashes need to be Windows-style (backslashes)? Also, is it a problem to have both the qt.conf and the env var setting line in my python code?

Ok, this is interesting. On the other Windows machine, after installing python-qt5 from git, it just worked in a python shell without any mucking with env vars or qt.conf. But on this machine, even doing the test in the original post on this thread didn't work, much less through pyinstaller.

jacksonofalltrades avatar Jan 27 '16 22:01 jacksonofalltrades

I'm really stumped and very late on this project. Can I pay you to give me phone support or something?

jacksonofalltrades avatar Jan 27 '16 22:01 jacksonofalltrades

Oh shit, sorry I've just head to bed over here.

There is a pretty good overview in the Qt docs about what should be in the qt.conf, but in general there should only need to be one path pointing to the main PyQt5 dir, containing all the dlls and exes. i think it was the bin path, but can't be sure and can't look it up as I'm quite indisposed at the moment.

Also its worth noting that the problem is not unique to this distribution or even PyQt, so all help on qt.conf, including that about C++, also applies here.

Also have a look at the wiki here, there's a page on updating this repo which includes a section about bundling.

Good luck and I'll try and have a look here again in the morning!

mottosso avatar Jan 27 '16 22:01 mottosso

Ok, thank you!

jacksonofalltrades avatar Jan 27 '16 22:01 jacksonofalltrades

Ok, I think I figured it out. I had to use 5.3.2, hopefully that'll work for what I need...

jacksonofalltrades avatar Jan 27 '16 23:01 jacksonofalltrades

Phew, glad to hear it worked out. :) Don't know what the difference is between the two versions, they should be identical, but I'll keep an eye out.

mottosso avatar Jan 28 '16 09:01 mottosso

setting QT_QPA_PLATFORM_PLUGIN_PATH works for me with 5.4.0

ruidc avatar Jun 20 '16 08:06 ruidc

I just ran into this when using libpython embedded in another program (for the PyCall package that lets us call Python from the Julia language). Since the executable was not running from the python.exe directory (and in fact, was not running python.exe at all), it was not finding the qt.conf file.

To improve the situation for "embedded" libpython users, instead of looking at the directory of the current executable, would you consider defaulting to sys.exec_prefix? (Or is it using sys.executable now?)

stevengj avatar Feb 04 '17 21:02 stevengj

(Note that on most platforms an embedding program can set sys.executable to be the location of python by calling Py_SetProgramName, but on Windows Python ignores this and always uses the name of the running program via GetModuleFileNameW. So, sys.executable is a problematic way to find qt.conf for embedded Python callers.)

stevengj avatar Feb 04 '17 21:02 stevengj

Hmm, I guess this is not under PyQt's control, since the location where qt.conf is loaded is set by Qt itself.

stevengj avatar Feb 04 '17 22:02 stevengj

That's right. qt.conf is entirely under the control of the Qt run-time and associated environment variables.

mottosso avatar Feb 06 '17 06:02 mottosso

Unfortunately, there seems to be no environment variable to tell it an alternate location from which to load qt.conf. I filed a Qt issue so that hopefully they can improve this, but for now we are parsing qt.conf ourselves and then using it to set the QT_PLUGIN_PATH environment variable.

stevengj avatar Feb 06 '17 13:02 stevengj

On a related note; I installed into a Virtualenv and had to move qt.conf from /path/to/virtualenv/ to /path/to/virtualenv/Scripts. I then updated contents to the following and it worked after that:

[Paths]
Prefix = ../Lib/site-packages/PyQt5
Binaries = ../Lib/site-packages/PyQt5

martinpengellyphillips avatar Jun 02 '17 11:06 martinpengellyphillips