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

[HOW TO] Set preferred binding from code before import #351

Closed
melMass opened this issue Nov 12, 2020 · 2 comments
Closed

[HOW TO] Set preferred binding from code before import #351

melMass opened this issue Nov 12, 2020 · 2 comments

Comments

@melMass
Copy link

melMass commented Nov 12, 2020

I'm pretty sure I already relied on that in the past but I cannot find anything in the readme about it.

I'm using Qt.py as a dependency in a library that might be used in both PyQt5 and PySide2.
I want to do something like this:

__qt__bindings__ = ["PyQt5","PySide2"]

import Qt
...
@MHendricks
Copy link
Collaborator

It's an environment variable. QT_PREFERRED_BINDING

import os
# Use os.path.pathsep for linux/windows compatibility. Linux uses `:` and windows uses `;`
os.environ['QT_PREFERRED_BINDING'] = os.path.pathsep.join(["PyQt5","PySide2"])

import Qt
import my_library.Qt
# Both Qt modules use the same binding
assert Qt.__binding__ == 'PyQt5'
assert my_library.Qt.__binding__ == 'PyQt5'

However if you are wanting to control this for your library and not cause problems if other libraries don't work well with PyQt5 you should use the newer environment variable so that only your Qt.py module attempts to use PyQt5. In this example you have a copy of Qt.py inside my_library.

import os
os.environ['QT_PREFERRED_BINDING_JSON'] = "{"my_library.Qt":["PyQt5","PySide2"]}"

import Qt
import my_library.Qt
# Only my_library.Qt uses the PyQt5 binding, other Qt modules work as normal
assert Qt.__binding__ == 'PySide2'
assert my_library.Qt.__binding__ == 'PyQt5'

I would recommend setting these variables globally for the session, not inside your library. I set QT_PREFERRED_BINDING_JSON as part of a plugin for Maya and startup scripts for 3ds Max, Nuke, etc.

@melMass
Copy link
Author

melMass commented Nov 12, 2020

Thanks a lot for the detailed answer!
I mainly wanted to do this for testing and to limit the library to PyQt5 & PySide2 Only

@melMass melMass closed this as completed Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants