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:
import foo
except ImportError:
logging.error('could not import foo')
sys.exit(1)

But logging is not configured at this point as my main() have not been
called yet.

Should I define a global variable and assign it to my module later? Or
should I let the exception happen and let the stack trace be the error
message?
 
M

Mel

Guillaume 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).

Actually, you can if you declare them global:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information..... global os
.... import os
....Traceback (most recent call last):
['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST',
'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE',
'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE',
'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT',
'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME',
'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK

etc.

Mel.
 
C

Chris Rebert

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:
   import foo
except ImportError:
   logging.error('could not import foo')
   sys.exit(1)

But logging is not configured at this point as my main() have not been
called yet.

Should I define a global variable and assign it to my module later? Or
should I let the exception happen and let the stack trace be the error
message?

If your users are technical, the latter, since it's much more
informative to anyone with programming/sysadmin skills. It's also not
really the sort of error your program can usefully recover from.
(Although in some cases, the module being imported may be considered
truly optional, in which case `try...except ImportError` makes sense;
but this is fairly uncommon unless a program is intended to have
extensions/plug-ins.)

If your users aren't technical, then you should have a top-level
try...except around almost the entire program that displays a simple
error message in the event of an unhandled exception, preferably with
an option to display the gory details (i.e. exception & stack trace).

Cheers,
Chris
 
G

Guillaume Martel-Genest

I did not think about using a global variable, and the top-level
try...except solution is interesting. After further thinking, I have
to reformulate my initial question:

How do I manage to run code before my imports?

For example, I want to make sure that I can use the logging module in
the case an import fails, so I want to call logging.basicConfig()
before this particular import. Likewise, I could want to import a
module whose path is relative to an environment variable, and would
want to test if this variable is set before doing so.

I have come up with 2 solution templates :
.... global foo
.... import foo.... pre_import()
.... import()
.... pass

To me, the latter looks better, but I could be missing something. In
any case, surrounding the entire program with try...except would look
like the following?
.... import logging
.... logging.basicConfig(format='%(message)s')
.... import foo
....
.... main().... pass
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top