Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt error when integrate pyqt4 and pywinauto #472

Closed
chenhuaizhen opened this issue Mar 8, 2018 · 12 comments
Closed

Qt error when integrate pyqt4 and pywinauto #472

chenhuaizhen opened this issue Mar 8, 2018 · 12 comments

Comments

@chenhuaizhen
Copy link

Environment

  • python 2.7
  • pyQt 4.11.4
  • pywinauto 0.6.4
  • windows 10

Detail

Hello all, I have a problem when integrate pyqt4 and pywinauto, the code like below:

from PyQt4 import QtGui
import sys
import pywinauto

def main():
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    w.resize(250, 150)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

And I get the error:

Qt: Could not initialize OLE (error 80010106)

If I remove "import pywinauto" and it can run normally.
thanks.

@vasily-v-ryabov
Copy link
Contributor

Hi @chenhuaizhen can you try pip install pywinauto==0.6.3 instead? We've made COM multithreading enabled for COM initialization and it could affect this.

@chenhuaizhen
Copy link
Author

@vasily-v-ryabov Yes, it works, many thanks for your help!

@vasily-v-ryabov
Copy link
Contributor

OK, it seems we should move COM initialization from import code to another place (when creating Application(backend="uia") probably with optional multithreading argument). Marking as a bug. @chenhuaizhen thanks for reporting this!

@vasily-v-ryabov
Copy link
Contributor

By the way sys.coinit_flags = 2 before import pywinauto should also help with pywinauto==0.6.4.
So you will not have to revert to 0.6.3.

We have one serious crash fixed in 0.6.4. So I would recommend the second solution.

@chenhuaizhen
Copy link
Author

@vasily-v-ryabov I have tried your new method with pywinauto==0.6.4, but got another warning, my code is:

from PyQt4 import QtGui
import sys
sys.coinit_flags = 2
import pywinauto

def main():
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QWidget()
    w.resize(250, 150)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

and the warning is:

UserWarning: Apply externally defined coinit_flags: 2
.format(module_sys.coinit_flags), UserWarning)

@vasily-v-ryabov
Copy link
Contributor

vasily-v-ryabov commented Mar 10, 2018

This is just a warning and it looks reasonable. But just to make an experiment... Can you call import pywinauto with no sys.coinit_flags modification after this line:

app = QtGui.QApplication(sys.argv)
import pywinauto

Is there some warnings?

@chenhuaizhen
Copy link
Author

@vasily-v-ryabov Sure, and my code is:

import sys
from PyQt4 import QtGui

def main():
    app = QtGui.QApplication(sys.argv)
    import pywinauto
    w = QtGui.QWidget()
    w.resize(250, 150)
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

and I got a warning:

UserWarning: Revert to STA COM threading mode
warnings.warn("Revert to STA COM threading mode", UserWarning)

And I have a question about the "sys.coinit_flags", although the warning looks reasonable, can I hide that warning when I run my program?

@vasily-v-ryabov
Copy link
Contributor

vasily-v-ryabov commented Mar 10, 2018

Try this code before import pywinauto:

import warnings
warnings.simplefilter("ignore", UserWarning)

Taken from here: https://stackoverflow.com/q/11175388/3648361

@chenhuaizhen
Copy link
Author

@vasily-v-ryabov Many thanks, it works

@vasily-v-ryabov
Copy link
Contributor

Well, I decided to re-qualify this thread as a question, because it's impossible to predict whether MTA or STA mode will be chosen by next import or statement. Let's leave MTA a default mode. Closing.

@sha-fuq
Copy link

sha-fuq commented May 1, 2019

Sorry, but what happened to this issue? Last year I used the hide warning fix you provided and left a comment in my code reminding me the extra lines were there temporarily till pywinauto 0.6.6. release.

It's been over a couple of months now and we've moved to the latest pywinauto version. But when I remove the temporary lines I still see the "pywinauto QWindowsContext: OleInitialize() failed: COM error..." warning is there.

Will there be a permanent fix for this in the future?

@vasily-v-ryabov
Copy link
Contributor

The latest comment in the thread should explain our decision. If we switch to STA as a default mode, it can break another library that wants to use MTA. And vice versa.

So permanent fix for all cases is impossible. Just import pywinauto later because it can adjust to COM threading mode already used by Qt4. While Qt4 can't do the same thing for pywinauto.

Removing the warning completely is also impossible, because this message is important at least first time. Just disable the warning if you don't want to see it. It's standard way Python provides. It can hide other UserWarnings, but it should be possible to write more tricky filter I believe.

Also the most important use cases involve interacting with another process, not with its own Python app. So we'd better concentrate on new features like UI recorder and Linux backend.

Thanks for understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants