我正在尝试运行别人的代码,我想它从未在Windows上运行过,而且我无法超越'git log | head -n 1 | awk '{print $2}'' 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 $2}'' 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 $2}'' 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 $2}'"
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 $2}'
正在产生错误,特别是错误255。我到处都在寻找有关错误代码的解释,但没有发现任何东西,这似乎很奇怪。文档不应该解释错误代码吗?
无论如何,我尝试只从提示符处运行该命令,然后我得到了:
(pytorch) C:\Users\mcram\Documents\Github\low-memory-fnn-training\apps\cifar10>git log | head -n 1 | awk '{print $2}'
'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的事实吗?我想知道这是否仅仅是因为我从命令行运行。在https://github.com/sr320/LabDocs/issues/664#issuecomment-318851496中,我还想知道问题是否出在我的PATH中没有Git Bash。但是,打开Git Bash并运行cat ~/.bash_profile
会给我cat: /c/Users/mcram/.bash_profile: No such file or directory
。但是,我确实知道了,但它似乎并未包含通向蟒蛇的路径:
$ 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命令行中运行。我的想法已经用完,可以使用一些帮助。谢谢。
答案 0 :(得分:1)
Python documentation says以下:
在shell = True的Windows上,COMSPEC环境变量指定默认的shell。
这意味着您可能会尝试调用cmd
而不是sh
或bash
。最好的选择是避免使用shell=True
,而是将cmd
变量设置为如下所示:
cmd = ["sh", "-c", cmd]
请注意,在Linux上,我们通常会编写/bin/sh
,而不是sh
,但是由于您使用的Windows无法正常工作。如果您觉得很重要,该解决方案还将可以移植到Linux和大多数其他Unix系统上。
一旦您使用sh
(我假设来自Windows的Git),它就应该为所有其他Unix命令设置适当的PATH。