[docs] [issue8557] subprocess PATH semantics and portability

Dave Abrahams report at bugs.python.org
Sun May 16 16:16:30 CEST 2010

Dave Abrahams <dave at boostpro.com> added the comment:

New data point: in some contexts on Windows (not sure of the exact cause but I was dealing with multiple drives), even this workaround isn't enough.  I ended up having to do something like this (i.e. manually search the path) on win32:

    def full_executable_path(invoked, environ):

        if os.path.splitext(invoked)[1]:
            return invoked
        explicit_dir = os.path.dirname(invoked)

        if explicit_dir:
            path = [ explicit_dir ]
            path = environ.get('PATH').split(os.path.pathsep)

        extensions = environ.get(
            # Use *something* in case the environment variable is
            # empty.  These come from my machine's defaults

        for dir in path:
            for ext in extensions:
                full_path = os.path.join(dir, invoked+ext)
                if os.path.exists( full_path ):
                    return full_path

        return invoked # Not found; invoking it will likely fail

    class Popen(subprocess.Popen):
        def __init__(
            self, args, bufsize=0, executable=None,
            stdin=None, stdout=None, stderr=None,
            preexec_fn=None, close_fds=False, shell=False, 
            cwd=None, env=None, 
            *args_, **kw):

            if executable is None and not shell:
                executable = full_executable_path(args[0], env or os.environ)

                args, bufsize, executable, stdin, stdout, stderr, 
                preexec_fn, close_fds, shell, cwd, env, *args_, **kw)


Python tracker <report at bugs.python.org>

More information about the docs mailing list