A tale of two execs

Christian Heimes lists at cheimes.de
Mon Feb 23 20:10:12 CET 2009


aha wrote
> def runner(cmd, stdin, stdout, ...):
>   try:
>     import subprocess
>     sbm = 1
>   except:
>     sbm = 0
> 
>   # Now do something
>   if sbm:
>     process = subporcess(...)
>   else:
>     import popen2
>     process = popen2.Popen4(...)
> 
> Has anyone else run into a situation similar to this one?

The canonical way for your try/except clause is:

try:
    import subprocess
except ImportError:
    subprocess = None

...

if subprocess is not None:
    ...
else:
    ...

Christian




More information about the Python-list mailing list