close

[Solved] Getting an error – AttributeError: ‘module’ object has no attribute ‘run’ while running subprocess.run([“ls”, “-l”])

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Getting an error – AttributeError: ‘module’ object has no attribute ‘run’ while running subprocess.run([“ls”, “-l”]) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Getting an error – AttributeError: ‘module’ object has no attribute ‘run’ while running subprocess.run([“ls”, “-l”]) Error Occurs?

Today I get the following error Getting an error – AttributeError: ‘module’ object has no attribute ‘run’ while running subprocess.run([“ls”, “-l”]) in python.

How To Solve Getting an error – AttributeError: ‘module’ object has no attribute ‘run’ while running subprocess.run([“ls”, “-l”]) Error ?

  1. How To Solve Getting an error – AttributeError: 'module' object has no attribute 'run' while running subprocess.run([“ls”, “-l”]) Error ?

    To Solve Getting an error – AttributeError: 'module' object has no attribute 'run' while running subprocess.run([“ls”, “-l”]) Error The subprocess.run() function only exists in Python 3.5 and newer.

  2. Getting an error – AttributeError: 'module' object has no attribute 'run' while running subprocess.run([“ls”, “-l”])

    To Solve Getting an error – AttributeError: 'module' object has no attribute 'run' while running subprocess.run([“ls”, “-l”]) Error The subprocess.run() function only exists in Python 3.5 and newer.

Solution 1

The subprocess.run() function only exists in Python 3.5 and newer.

It is easy enough to backport however:

def run(*popenargs, **kwargs):
    input = kwargs.pop("input", None)
    check = kwargs.pop("handle", False)

    if input is not None:
        if 'stdin' in kwargs:
            raise ValueError('stdin and input arguments may not both be used.')
        kwargs['stdin'] = subprocess.PIPE

    process = subprocess.Popen(*popenargs, **kwargs)
    try:
        stdout, stderr = process.communicate(input)
    except:
        process.kill()
        process.wait()
        raise
    retcode = process.poll()
    if check and retcode:
        raise subprocess.CalledProcessError(
            retcode, process.args, output=stdout, stderr=stderr)
    return retcode, stdout, stderr

There is no support for timeouts, and no custom class for completed process info, so I’m only returning the retcodestdout and stderr information. Otherwise it does the same thing as the original.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read