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


Groups > comp.lang.python > #8182

Re: Handling import errors

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'preferably': 0.03; 'assign': 0.04; '(although': 0.05; 'main()': 0.05; 'exception,': 0.07; 'importerror:': 0.07; 'imports': 0.07; 'skip:` 10': 0.07; '21,': 0.09; 'foo': 0.09; 'top-level': 0.09; 'pm,': 0.10; 'exception': 0.12; 'wrote:': 0.14; 'latter,': 0.16; 'pythonic': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'subject:import': 0.16; 'uncommon': 0.16; 'unhandled': 0.16; 'stack': 0.16; '(i.e.': 0.17; 'cc:addr :python-list': 0.17; 'tue,': 0.17; 'trace': 0.19; 'yet.': 0.19; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; "aren't": 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'thus': 0.23; 'function': 0.25; 'script': 0.27; 'message- id:@mail.gmail.com': 0.28; 'import': 0.29; 'cc:addr:python.org': 0.30; 'fairly': 0.30; 'module': 0.30; 'imported': 0.30; 'skip:\xc2 20': 0.30; 'sort': 0.31; 'define': 0.31; 'anyone': 0.32; "can't": 0.32; 'configured': 0.32; 'error': 0.33; 'chris': 0.34; 'option': 0.35; 'like:': 0.35; 'try:': 0.35; 'considered': 0.36; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'logging': 0.37; 'received:209.85.213': 0.37; 'skip:e 20': 0.37; 'case': 0.37; 'skills.': 0.37; 'but': 0.38; 'cases,': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'called': 0.39; 'unless': 0.39; 'received:209': 0.39; 'really': 0.40; 'happen': 0.60; 'almost': 0.60; 'more': 0.60; 'your': 0.60; 'details': 0.64; 'skip:\xc2 10': 0.72; 'informative': 0.73; 'message?': 0.84; 'sender:addr:chris': 0.84; 'from.': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=/8RUVYAFvPdaAJSQ68/ZlwhVHQwDmhcvzs5wSLFkAhg=; b=KhJts6p91PvOKCZtBcm3Q8ys4PdsByXHF4T9zsE6ukxu4sUty948z86zXqDwgs33s5 QXFGmNcHSn6BDRXBDKnnqLcsuHdTTWt2qedoOeVGn30oUs0vHD025QlxNMygEG1Xb1Pc wU+Dm9Lk4zvd5bZSePgAdALzbuVukuv9bKEgQ=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=A4snzVB8MB4zQZmDEXbrCXqaAmEh9Iy7zaYjOtH60duPiRtDryRzfRrnGhSN2gsrtg Cn88yjjSx1fN8jeDWRogsFLzHbQF2UV0xlfigU4GivVdfEoySGTXRCTNUST2pGp+xIfq m+6DAIC5IUYy6YWJAgb0gGzmHV1JDhJ64syaQ=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <ac1909f8-4e16-4094-92a4-5239122e1511@j31g2000yqe.googlegroups.com>
References <ac1909f8-4e16-4094-92a4-5239122e1511@j31g2000yqe.googlegroups.com>
Date Tue, 21 Jun 2011 22:12:59 -0700
X-Google-Sender-Auth N93l5TewHjTlFPVSIMREMwK9w6c
Subject Re: Handling import errors
From Chris Rebert <clp2@rebertia.com>
To Guillaume Martel-Genest <guillaumemg@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.270.1308719582.1164.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 82.94.164.166
X-Trace 1308719582 news.xs4all.nl 49179 [::ffff:82.94.164.166]:43784
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8182

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web