We are building a library, and part of that is a module that provides different database interfaces on different systems.
class DbInterface1(object):
"""Uses MySQLdb, and is useful on system A."""
def __init__():
pass
class DbInterface2(object):
"""Uses SQLAlchemy, and is useful on system B."""
def __init__():
pass
We'd like to skip imports that aren't used. For instance, on a system that has MySQLdb, we will use DbInterface1 and should not require SQLAlchemy to be installed. That is, on system A, we would use it like this:
from ourlibrary.dbinterfaces import DbInterface1
...
Obviously, this will not work:
import MySQLdb
import sqlalchemy
class DbInterface1(object):
"""Uses MySQLdb, and is useful on system A."""
def __init__():
pass
class DbInterface2(object):
"""Uses SQLAlchemy, and is useful on system B."""
def __init__():
pass
We assumed that moving the imports into the classes that use them would work, since the library client won't import something it's not using, but it still has failed dependencies.
class DbInterface1(object):
"""Uses MySQLdb, and is useful on system A."""
import MySQLdb
def __init__():
pass
class DbInterface2(object):
"""Uses SQLAlchemy, and is useful on system B."""
import sqlalchemy
def __init__():
pass
What is the correct way to do this? Is it even possible?