Handling import errors

Tim Johnson tim at johnsons-web.com
Tue Jun 21 17:10:16 EDT 2011


* Guillaume Martel-Genest <guillaumemg at gmail.com> [110621 12:53]:
> What is the pythonic way to handle imports error? What is bugging me
> is that the imports can't be inside a function (because I use them in
> different places in the script and thus they have to be in the global
> scope). I would write something like:
  Suppose you do something like this:
  try :
    mod = __import__('mymodulename')
  except ImportError:
    pass ## replace with error handling here 

  so `mod' is global if you execute the above code as top-level,
  *but* you can also pass `mod' as an argument like any other
  variable, as far as I know and far as I have done.

-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com



More information about the Python-list mailing list