I would like to do something like this in one source file, QT.py:
import sys
import PyQt4
sys.modules["Qt"] = PyQt4
Then import this file in the other source files, and use it like this:
import QT
from Qt.QtCore import *
So I can change from PyQt4 to PySide in QT.py without touching all the source files (with a possibly ugly sed script) These modules are mostly API compatibile and I would like to test them both. Is there an easy way to do this? (Because the ways I tried are not working)
Maybe the I need imp
module, but it seems too low level.
update: Figured out method more in line with your requirements:
You can structure your pseudo-module as:
Where
Qt/__init__.py
is:Qt/QtCore/__init__.py
is:Qt/QtGui/__init__.py
is:Then, in your code, you can reference it as follows:
I highly recommend against using
from Qt.QtGui import *
in your code as importing everything is considered bad form in Python since you lose all namespaces in the process.update: I like Ryan's suggestion of conditional imports. I'd recommend combining that into the above code. For example:
Qt/QtGui/__init__.py
:You can conditionally import libraries. Here is a bit of a hacky example, where you check for a command-line argument of "PyQt4":
Use an import hook:
Output: