Handling import errors

  • Thread starter Guillaume Martel-Genest
  • Start date
G

Guillaume Martel-Genest

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:

try:
 
T

Tim Johnson

* Guillaume Martel-Genest said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top