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: 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: References: Date: Tue, 21 Jun 2011 22:12:59 -0700 X-Google-Sender-Auth: N93l5TewHjTlFPVSIMREMwK9w6c Subject: Re: Handling import errors From: Chris Rebert To: Guillaume Martel-Genest 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Jun 21, 2011 at 1:51 PM, 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). I would write something like: > > try: > =C2=A0 =C2=A0import foo > except ImportError: > =C2=A0 =C2=A0logging.error('could not import foo') > =C2=A0 =C2=A0sys.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