Subprocess module fails to run command

前端 未结 3 532
说谎
说谎 2021-01-13 23:25

I\'m trying to execute Google\'s cpplint.py on a group of my files and collect the results to one log file. However, I have not managed to beat the subprocess module. My cur

3条回答
  •  借酒劲吻你
    2021-01-14 00:22

    Looks like cpplint.py is simply exiting with a non-zero return code - which it might do, for instance, if it finds errors or "lint" in the source files it is checking.

    See the documentation for subprocess.check_output. Note that if the command executed returns a non-zero exit code then a subprocess.CalledProcessError is raised.

    You could work around it by watching for CalledProcessError, e.g.

    try:
        output = subprocess.check_output(cmd)
    except subprocess.CalledProcessError as e:
        # ack!  cpplint.py failed... report an error to the user?
    

    EDIT:

    The SyntaxError seems to be the key here, and is probably caused by C:\Python32\lib being in your PYTHONPATH (either explicitly, or, this could happen if it is your current working directory).

    The Python interpreter (since about 1.5.2-ish) automatically runs import site when started. So, when this is the case, and your script goes to execute:

    c:/Python27/python.exe C:/users/me/Documents/dev/cpplint.py ...
    

    then the Python 2.7 interpreter will find C:\Python32\lib\site.py first, and try to load that, instead of the one (presumably) at C:\Python27\lib\site.py. The issue is that Python 3's site.py contains syntax incompatible with Python 2, so the process launched by subprocess.check_output is failing with a SyntaxError before it even gets a chance to run cpplint, which propagates the CalledProcessError.

    Solution? Make sure Python2 gets a bonafide Python2 "PYTHONPATH", and likewise for Python3! In other words, make sure C:\Python32\lib is not in the PYTHONPATH search path when running the Python2 interpreter.

    One way to do this in your case is to set an explicit environment when launching the process, e.g.:

    python2_env = {"PYTHONPATH": "path/to/python2/stuff:..."}
    output = subprocess.check_output(cmd, env=python2_env)
    

提交回复
热议问题