Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8129 > unrolled thread

Handling import errors

Started byGuillaume Martel-Genest <guillaumemg@gmail.com>
First post2011-06-21 13:51 -0700
Last post2011-06-22 18:52 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Handling import errors Guillaume Martel-Genest <guillaumemg@gmail.com> - 2011-06-21 13:51 -0700
    Re: Handling import errors Mel <mwilson@the-wire.com> - 2011-06-21 17:12 -0400
    Re: Handling import errors Chris Rebert <clp2@rebertia.com> - 2011-06-21 22:12 -0700
      Re: Handling import errors Guillaume Martel-Genest <guillaumemg@gmail.com> - 2011-06-22 18:52 -0700

#8129 — Handling import errors

FromGuillaume Martel-Genest <guillaumemg@gmail.com>
Date2011-06-21 13:51 -0700
SubjectHandling import errors
Message-ID<ac1909f8-4e16-4094-92a4-5239122e1511@j31g2000yqe.googlegroups.com>
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?

[toc] | [next] | [standalone]


#8133

FromMel <mwilson@the-wire.com>
Date2011-06-21 17:12 -0400
Message-ID<itr1gu$dhf$1@speranza.aioe.org>
In reply to#8129
Guillaume Martel-Genest wrote:

> 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.
>>> def f():
...     global os
...     import os
... 
>>> dir (os)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> f()
>>> dir (os)
['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.

[toc] | [prev] | [next] | [standalone]


#8182

FromChris Rebert <clp2@rebertia.com>
Date2011-06-21 22:12 -0700
Message-ID<mailman.270.1308719582.1164.python-list@python.org>
In reply to#8129
On Tue, Jun 21, 2011 at 1:51 PM, Guillaume Martel-Genest
<guillaumemg@gmail.com> wrote:
> 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
--
http://rebertia.com

[toc] | [prev] | [next] | [standalone]


#8254

FromGuillaume Martel-Genest <guillaumemg@gmail.com>
Date2011-06-22 18:52 -0700
Message-ID<6329f694-602a-4d15-b0e1-eaa0a0a5a28d@c20g2000vbv.googlegroups.com>
In reply to#8182
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 :

>>> import logging
>>>
>>> main()
>>>
>>> def pre_import():
...     logging.basicConfig(format='%(message)s')
>>>
>>> def import():
...     global foo
...     import foo
>>>
>>> def main():
...     pre_import()
...     import()

>>> import logging
>>> logging.basicConfig(format='%(message)s')
>>> import foo
>>>
>>> main()
>>>
>>> def main():
...     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?

>>> try:
...     import logging
...     logging.basicConfig(format='%(message)s')
...     import foo
...
...     main()
>>> except Exception:
...     # Display simple error message
>>>
>>> def main():
...     pass

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web