Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'flags': 0.05; 'importerror:': 0.07; 'python': 0.08; 'block.': 0.09; 'correct.': 0.09; 'disable': 0.09; 'propagated': 0.09; 'broken': 0.12; 'exception': 0.12; 'cc:addr:python-list': 0.15; 'syntax': 0.15; 'except:': 0.16; 'exceptions).': 0.16; 'installed,': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; 'wrap': 0.18; '(which': 0.19; 'possibly': 0.21; 'trying': 0.21; 'cc:no real name:2**0': 0.21; "doesn't": 0.23; 'received:209.85.212.46': 0.23; 'received :mail-vw0-f46.google.com': 0.23; 'module,': 0.23; 'header:In- Reply-To:1': 0.23; 'cc:2**0': 0.25; 'code': 0.25; 'code,': 0.27; 'sound': 0.28; 'import': 0.28; 'pass': 0.28; 'importing': 0.28; 'module.': 0.28; 'cc:addr:python.org': 0.29; 'lines': 0.30; 'module': 0.30; '(maybe': 0.30; 'elsewhere': 0.30; 'imported': 0.30; 'chris': 0.30; 'installed': 0.30; 'error': 0.31; 'nov': 0.31; 'pm,': 0.31; 'version': 0.31; 'does': 0.32; 'list': 0.32; "won't": 0.32; "i've": 0.33; 'it.': 0.33; 'someone': 0.33; 'there': 0.33; 'fail': 0.34; 'message-id:@gmail.com': 0.34; 'received:209.85.212': 0.34; 'probably': 0.34; 'try:': 0.34; 'project': 0.35; 'rather': 0.35; 'example,': 0.36; 'but': 0.37; 'using': 0.37; 'received:google.com': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'received:192': 0.38; 'e.g.': 0.38; "i'd": 0.39; 'should': 0.39; 'except': 0.39; "there's": 0.39; "it's": 0.39; 'got': 0.39; 'subject:: ': 0.39; 'might': 0.39; 'skip:_ 10': 0.40; 'received:209': 0.40; 'port': 0.40; 'happen': 0.61; 'your': 0.61; '2011': 0.62; 'header:Message-Id:1': 0.62; 'alternative': 0.63; 'ports': 0.67; 'serial': 0.71; '100%': 0.82; 'absence,': 0.84; 'received:192.168.8': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=6CPV0XduOnufQAUNvcAFwg1RMewj+CQuf23rLJ8kuM4=; b=kmPbOtf5HycYwJzTTD++s77pQzdgwrP/Zge1VJlgVsK8QIMBFpB5gOe7SqpySz4cB0 BNopl0v0DXVLgvUmnnbxGmCSYoh+C8xU+ar9rhCPTQAfZgoTRx7u2tZhRGpiA/wbQPpr 6PcCsg8r+O05/KpRjx3i4h/msWfZBPAVOiylI= Subject: Re: suppressing import errors Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=iso-8859-1 From: David Riley In-Reply-To: Date: Tue, 15 Nov 2011 12:57:11 -0500 Content-Transfer-Encoding: quoted-printable References: To: Andreea Babiuc X-Mailer: Apple Mail (2.1251.1) 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321379836 news.xs4all.nl 6943 [2001:888:2000:d::a6]:32846 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15726 On Nov 15, 2011, at 12:35 PM, Andreea Babiuc wrote: >=20 >=20 > On 15 November 2011 17:24, Chris Kaynor = wrote: > As with any Python code, you can wrap the import into a try: except = block. >=20 > try: > import badModule > except: >=20 > =20 > pass # Or otherwise handle the exception - possibly importing an > alternative module. >=20 >=20 > Hmm, I know this might sound silly, but if it fails I still want to = import the module and disable those lines of code that are related to = the reason while the module failed to be imported in the first place. = Even if that makes the code not 100% correct. >=20 > Does that make sense ? It makes sense. It probably also makes sense to only do an "except = ImportError", since if there are other errors (say, syntax errors in a = module you're trying to import, rather than its absence, you probably = want to know about it. To disable code that won't work without the module you're trying to = import, you can always set flags in your module. For example, I've got = a project at work that can use a variety of communications interfaces, = including using PySerial for serial port comms. But if someone doesn't = have PySerial installed, I want it to fail gracefully and just not = support serial. So I can do the following: try: import serial _serial_enabled =3D True except ImportError: print("PySerial not installed - serial ports not supported!") _serial_enabled =3D False And then elsewhere in my module, I can check the value of = _serial_enabled to see if I should e.g. list the serial ports in = available communications interfaces. Of course, if there's some other error in PySerial (maybe I installed a = broken version with a syntax error?), that error will get propagated up, = which is a good thing, because I'd rather know that PySerial is broken = than just have it tell me it's not installed (which is what would happen = if I simply caught all exceptions). Your mileage may vary. - Dave