본문 바로가기
파이썬

파이썬 오류 발생-AttributeError : subprocess.run ([ "ls", "-l"])을 실행하는 동안 'module'개체에 'run'속성이 없습니다.

by º기록 2020. 10. 26.
반응형

저는 AIX 6.1에서 실행 중이고 Python 2.7을 사용하고 있습니다. 다음 줄을 실행하고 싶지만 오류가 발생합니다.

subprocess.run(["ls", "-l"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'run'

 

해결 방법

 


그러나 백 포트하는 것은 쉽습니다.

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

타임 아웃에 대한 지원이없고 완료된 프로세스 정보에 대한 사용자 정의 클래스가 없으므로 retcode , stdout stderr 정보 만 반환합니다. . 그렇지 않으면 원본과 동일한 작업을 수행합니다.

 

참조 페이지 https://stackoverflow.com/questions/40590192

 

 

반응형

댓글