subprocess.CalledProcessError: ... returned non-zero exit status 255

subprocess.CalledProcessError: ... returned non-zero exit status 255

我正在尝试 运行 别人的代码,我想这在 Windows 上从来没有 运行,我无法通过 'git log | head -n 1 | awk '{print }'' returned non-zero exit status 255

Logging to ./logs/log_j0_t0.txt
Traceback (most recent call last):
  File "C:\Users\mcram\Documents\Github\low-memory-fnn-training\napoleon\utils\log_utils.py", line 55, in log_git_commit_info
    stderr=subprocess.PIPE)
  File "C:\Users\mcram\Anaconda3\envs\pytorch\lib\subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'git log | head -n 1 | awk '{print }'' returned non-zero exit status 255.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 86, in <module>
    main()
  File "main.py", line 23, in main
    init_log(args)
  File "C:\Users\mcram\Documents\Github\low-memory-fnn-training\napoleon\utils\log_utils.py", line 36, in init_log
    log_git_commit_info()
  File "C:\Users\mcram\Documents\Github\low-memory-fnn-training\napoleon\utils\log_utils.py", line 62, in log_git_commit_info
    returncode, err_type=subprocess.CalledProcessError, cmd=cmd)
  File "C:\Users\mcram\Documents\Github\low-memory-fnn-training\napoleon\utils\log_utils.py", line 109, in log_error
    raise err_type(msg, **kwargs)
subprocess.CalledProcessError: Command 'git log | head -n 1 | awk '{print }'' returned non-zero exit status 255.

我 运行 宁 Windows 10 并已安装 git version 2.14.2.windows.1。下面是来自 log_utils.py 的相关函数。第 55 行是 stderr=subprocess.PIPE).

def log_git_commit_info():
    get_commit_hash = "git log | head -n 1 | awk '{print }'"
    check_unstaged = 'git diff --exit-code'
    check_staged = 'git diff --cached --exit-code'
    status = 'git status'
    cmds = [get_commit_hash, check_unstaged, check_staged, status]
    do_checks = [True, False, False, True]
    saved_infos = []
    for cmd, do_check in zip(cmds, do_checks):
        try:
            process_result = subprocess.run(
                cmd,
                shell=True,
                check=do_check,
                universal_newlines=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
            saved_infos.append((process_result.returncode,
                                process_result.stdout.strip()))
        except subprocess.CalledProcessError as e:
            err_msg = str(e)
            returncode = int(err_msg.split()[-1][:-1])
            log_error(
                returncode, err_type=subprocess.CalledProcessError, cmd=cmd)
    commit_hash = saved_infos[0][1]
    log_info('Current commit: ' + commit_hash)
    if saved_infos[1][0] or saved_infos[2][0]:
        log_warn('Uncommitted changes present!')
        log_warn("Output of 'git status':\n" + saved_infos[3][1])
        log_info("Output of 'git diff HEAD':")
        diff_output = subprocess.run(
            'git diff HEAD',
            shell=True,
            check=do_check,
            universal_newlines=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        log_info(diff_output.stdout)

我唯一的猜测是 git log | head -n 1 | awk '{print }' 产生了错误,特别是错误 255。我到处寻找错误代码的一些解释,但没有找到任何东西,这看起来很奇怪。文档不应该解释错误代码吗?

无论如何,我只是 运行 从提示符中输入了那个命令,我得到了这个:

(pytorch) C:\Users\mcram\Documents\Github\low-memory-fnn-training\apps\cifar10>git log | head -n 1 | awk '{print }'
'head' is not recognized as an internal or external command,
operable program or batch file.

'head' is not recognized as an internal or external command 会不会是错误 255?我想知道这是否只是因为我 运行ning 从命令行。来自 https://github.com/sr320/LabDocs/issues/664#issuecomment-318851496 我也想知道问题是否是 Git Bash 不在我的 PATH 中。但是,打开 Git Bash 和 运行 宁 cat ~/.bash_profile,给我 cat: /c/Users/mcram/.bash_profile: No such file or directory。但是,我确实得到了这个,它似乎不包含通往 Anaconda 的路径:

$ echo $PATH
/c/Users/mcram/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/mcram/bin:/c
/ProgramData/DockerDesktop/version-bin:/c/Program Files/Docker/Docker/Resources/bin:/c/Program 
Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin:/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA
/v10.1/libnvvp:/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/bin:/c/Program Files/NVIDIA GPU 
Computing Toolkit/CUDA/v8.0/libnvvp:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows
/System32/WindowsPowerShell/v1.0:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/Program 
Files/PuTTY:/cmd:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32
/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files/NVIDIA Corporation/Nsight Compute 
2019.3.0:/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/c/Program Files/Intel
/Intel(R) Management Engine Components/DAL:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:
/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Users/mcram/AppData/Local
/Microsoft/WindowsApps:/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/extras/CUPTI/libx64:
/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64:/c/Users/mcram/AppData/Local
/GitHubDesktop/bin:/c/Users/mcram/AppData/Local/Microsoft/WindowsApps:/usr/bin/vendor_perl:/usr/bin
/core_perl

如果我找不到 .bash_profile 我不知道如何更新 PATH,即使我能找到它,我也不知道该把什么放在那里,因为我'我不是在 Jupyter 中 运行 脚本,而是在 Python 命令行。我 运行 没有想法,需要一些帮助。谢谢。

Python documentation says以下:

On Windows with shell=True, the COMSPEC environment variable specifies the default shell.

这意味着您可能正在尝试调用 cmd 而不是 shbash。你最好的选择是避免 shell=True 并将你的 cmd 变量设置为如下所示:

cmd = ["sh", "-c", cmd]

请注意,在 Linux 上,我们通常会写成 /bin/sh,而不是 sh,但由于您使用的是 Windows,这将不起作用。这个解决方案也可以移植到 Linux 和大多数其他 Unix 系统,以防你很在意。

一旦你使用了sh(我从Git假设Windows),它应该为所有其他Unix命令适当地设置PATH。