Dan Stromberg wrote:

Is there a way of making code like this pass pylint? It's probably clear from the code, but I'll say it anyway: I'm writing Python code that'll run on multiple python interpreters, but none of those interpreters have all 100% of these modules:

try:
   # python 3
   import dbm.gnu as db_mod
except ImportError:
   # python 2
   try:
      import gdbm_ctypes as db_mod
      print('Using gdbm_ctypes')
   except ImportError:
      try:
         import gdbm as db_mod
      except ImportError:
# this gets something that's kind of like bsddb on pypy 1.3 via ctypes
         import dbm as db_mod

Thanks!

--
Dan Stromberg

Which error are you talking about ? The code above doesn't raise any issue with pylint (not with my configuration).
Would it be about unused imports ?

Anyway, another way to write the above code, avoiding that many nested try blocks (not tested):

_MODULES = ['dbm.gnu', 'gdbm_ctypes', 'gdbm', 'dbm'] # order matters !
for mod in _MODULES:
   try:
       db_mod = __import__(mod, globals(), locals(), [], -1)
       print 'using %s' % mod
       break
   except ImportError:
       continue
else:
   raise ImportError('No module among %s can be imported' % _MODULES)


That could fix your pylint issue in the meantime.

JM
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to