A tale of two execs

bieffe62 at gmail.com bieffe62 at gmail.com
Mon Feb 23 18:06:25 CET 2009


On Feb 23, 5:53 pm, aha <aquil.abdul... at gmail.com> wrote:
> Hello All,
>   I am working on a project where I need to support versions of Python
> as old as 2.3. Previously, we distributed Python with our product, but
> this seemed a bit silly so we are no longer doing this.  The problem
> that I am faced with is that we have Python scripts that use the
> subprocess module, and subprocess is not available in Python 2.3.
> Below is the strategy I am thinking about using, however if, you have
> better ideas please let me know.
>
> 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?

IIRC,  subprocess is a pure python module. If so, tou could try if
subprocess compiles under 2.3.
If so, or if it has easily fixable problems, you could rename it as
someting like 'backported_subprocess.py'
and include it in your app, which then should do:

try:
   import subprocess
except ImportError:
   import backported_subprocess as subprocess

This should minimize the change at your code. Of course if the usage
of subprocess is restricted
to few functions, your approach to provide two alternative
implementations of those function
is also workable. However, check if/when the module popen2, which is
deprecated, is planned for
remioval, otherwise you will have the same problem later.

HTH

Ciao
------
FB



More information about the Python-list mailing list