Selective importing and package dependencies

Chris Rebert clp at rebertia.com
Sun Sep 28 15:23:22 EDT 2008


On Sun, Sep 28, 2008 at 7:10 AM, David Pratt <fairwinds.dp at gmail.com> wrote:
> Hi. I am in the midst of preparing a package to convert between various
> schemas including orms. The issue is I don't want django, slqalchemy, storm,
> rdflib etc. as hard dependencies of the package. Each module is a schema to
> schema conversion. As an example, I have imports for sqlalchemy with classes
> and methods that use them.
>
> from sqlalchemy.util import OrderedDict
> from sqlalchemy import types as rdbtype
> import sqlalchemy as sa
>
> I have my own ideas about how I might do this but looking for
> recommendations from others how they would handle this so the result would
> be:
>
> 1. no hard dependencies on any of these other packages
> 2. load the module without failure.
> 3. import the dependent package if available to perform the conversion

Why not just catch the ImportError-s and set some variables depending
on whether you were able to import the modules, e.g.:

# repeat for each soft-depended module
try:
    import django_module
except ImportError:
    imported_django = False
else:
    imported_django = True

#later in the file
if imported_django and imported_sqlalchemy:
    class DjangoToSqlAlchemy(object):
        #code that uses the django and sqlalchemy modules here

Regards,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> Many thanks
> David
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list