We keep a python library for various code at work. It has been a great way to share code, implement things only once, stay consistent, etc. Parts of it depends on external libraries, and a there have been a couple soulutions to this, none of which feel right.
One solution was to include the entire external library in our library. The argument was that the external library's version can be controlled, but this adds a bunch of code, is ugly and doesn't feel pythonic.
Another solution was a try import
with a print error if the import fails. This feels good, is pep8, but our library has a large variety of modules and you'll often need one part of it, but not another. With this solution you'll see an error when doesn't affect you. There have been complaints.
try:
import zmq
import simplejson
except:
print "Install zmq and simplejson to use the LiveFeedSubscription()."
I proposed putting try imports
into the Class __init__
(solves the above problem), but it is not pep8; and arguably makes it harder to tell what the dependencies are as they are not being imported at the top of the module.
def __init__(self, msg_type='', msg_types=[], debug=False):
try:
import zmq
import simplejson
except:
print "Install zmq and simplejson to use the LiveFeedSubscription()."
What is the python best practice for including external libraries in a library with such greatly divergent modules?