Re: Testing the availability of a module

2005-12-19 Thread Erik Max Francis
Bo Peng wrote: > Is there a better way than doing > > try: >import aModule > except: >has_aModule = False > else: >has_aModule = True > > The main concern here is that loading aModule is unnecessary (and may > take time). Yes. Specifically catch ImportError in your except clause.

Re: Testing the availability of a module

2005-12-19 Thread Peter Otten
Bo Peng wrote: > def GUI(): >if options['cml']: > cml() >else: > if imp.find_module('wx'): >wxGUI() > elif imp.find_module('Tkinter'): >tkGUI() > else: >cml() On the other hand, I don't see how this is superior to an exception-based approach...

Re: Testing the availability of a module

2005-12-19 Thread Fredrik Lundh
Bo Peng wrote: > > here's an outline that matches your use case: > > > > if user wants command line: > > command line only > > else: > > if wxPython is available: > > prepare wx based GUI > > elif Tkinter is available: > > prepare tk based GU

Re: Testing the availability of a module

2005-12-19 Thread Bo Peng
> if the user happens to prefer the command line interface, why bother looking > for a GUI ? sounds like you haven't really thought this through... > > here's an outline that matches your use case: > > if user wants command line: > command line only > else: > if wxPython

Re: Testing the availability of a module

2005-12-19 Thread Fredrik Lundh
Bo Peng wrote: > > And if you aren't doing the above multiple times, then you *really* > > shouldn't be worried about the time to load "aModule" > > This is used in a scenario like: > >if wxPython is available: > prepare wx based GUI >elif Tkinter is available: > prepare tk bas

Re: Testing the availability of a module

2005-12-19 Thread Steven D'Aprano
On Mon, 19 Dec 2005 08:33:18 -0600, Bo Peng wrote: > This is used in a scenario like: > >if wxPython is available: > prepare wx based GUI >elif Tkinter is available: > prepare tk based GUI >else: > command line only > > Since a user may prefer command line so wxPython/

Re: Testing the availability of a module

2005-12-19 Thread Bo Peng
Peter Hansen wrote: > Bo Peng wrote: > >> Is there a better way than doing >> >> try: >>import aModule >> except: >>has_aModule = False >> else: >>has_aModule = True >> >> The main concern here is that loading aModule is unnecessary (and may >> take time). > And if you aren't doing

Re: Testing the availability of a module

2005-12-19 Thread Peter Hansen
Bo Peng wrote: > Is there a better way than doing > > try: >import aModule > except: >has_aModule = False > else: >has_aModule = True > > The main concern here is that loading aModule is unnecessary (and may > take time). Loading a module does *not* take time after the first time.

Re: Testing the availability of a module

2005-12-19 Thread Steven D'Aprano
On Mon, 19 Dec 2005 02:52:08 -0600, Bo Peng wrote: > Dear list, > > Is there a better way than doing > > try: >import aModule > except: >has_aModule = False > else: >has_aModule = True Do you mean to catch every possible exception when importing aModule? If you replace "except:" wi

Re: Testing the availability of a module

2005-12-19 Thread Robert Kern
Gerhard Häring wrote: > Bo Peng wrote: > >>Dear list, >> >>Is there a better way than doing >> >>try: >> import aModule >>except: >> has_aModule = False >>else: >> has_aModule = True >> >>The main concern here is that loading aModule is unnecessary (and may >>take time). > > No, there is n

Re: Testing the availability of a module

2005-12-19 Thread Thomas Heller
Gerhard Häring <[EMAIL PROTECTED]> writes: > Bo Peng wrote: >> Dear list, >> Is there a better way than doing >> try: >>import aModule >> except: >>has_aModule = False >> else: >>has_aModule = True >> The main concern here is that loading aModule is unnecessary (and >> may take time).

Re: Testing the availability of a module

2005-12-19 Thread Gerhard Häring
Bo Peng wrote: > Dear list, > > Is there a better way than doing > > try: >import aModule > except: >has_aModule = False > else: >has_aModule = True > > The main concern here is that loading aModule is unnecessary (and may > take time). No, there is not really a better way. You *c

Re: Testing the availability of a module

2005-12-19 Thread Kevin Yuan
2005/12/19, Bo Peng <[EMAIL PROTECTED]>: Dear list,Is there a better way than doingtry:   import aModuleexcept:   has_aModule = Falseelse:   has_aModule = TrueThe main concern here is that loading aModule is unnecessary (and may take time).If loading aModule is unnecessary, the best way is not load