George Oblapenko
George Oblapenko

Reputation: 878

Organizing optional imports in Python

I'm working on a Python 3 project which involves writing data. Now, I want the user to be able to choose a format for the data to be written in – pickle, SQLite, PostgreSQL, etc. Well, pickle and sqlite support is built into Python, so no worries there. Other stuff needs importing other modules, though.

The project, without going into unnecessary detail, consists of the following: A class ProjectClass which has read, write, etc. methods, which write data to columns, create new columns etc. So the user chooses the backend when creating an object of the ProjectClass type:

from project import ProjectClass
instance = ProjectClass(backend='MySQL')
instance.write(column_name='column1', data=some_data)

And the backend handles all the database stuff (so the ProjectClass is database-agnostic). The read, write and other methods call functions from backends.py, and the function they call depends on the backend type passed to the ProjectClass instance when creating it.

Now, I can't fully understand how to:

  1. not have the user install every package for every backend (since, hopefully, the project will grow and support more and more DB backends)
  2. check if the necessary package is installed (psycopg2 for PostgreSQL, etc)

Should I split the backends into separate files for each database types, and when the user creates a ProjectType instance, it tries to import the necessary file (postgresqlbackend.py for PostgreSQL, mysqlbackend.py for MySQL, etc.) and see if the import fails (due to absence of packages that are imported in the <backendname>backend.py file)?

Upvotes: 4

Views: 1008

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59701

Should I split the backends into separate files for each database types, and when the user creates a ProjectType instance, it tries to import the necessary file

This sounds like a good idea. Don't forget however that you can also import a module anywhere (it can be inside a function - it doesn't need to be at the top of file). Then you can surround this import statement with a try/except and warn the user beforehand.

try:
    import non_existant_module
except ImportError as e:
    print e.message # prints "No module named non_existant_module"

The best approach IMO is to print this error when your program first starts after loading the runtime config specifying which module/database type will be used. That way the user is instantly notified.

Upvotes: 3

Related Questions