subprocess.CalledProcessError: Command ‘[‘git‘, ‘describe‘]‘ returned non-zero exit status 128

Q:在跑CenterTrack的时候遇到了下面问题:
 

  File "main.py", line 101, in <module>
    main(opt)
  File "main.py", line 37, in main
    logger = Logger(opt)
  File "/home/xxx/CenterTrack/src/lib/logger.py", line 33, in __init__
    subprocess.check_output(["git", "describe"])))
  File "/home/xx/anaconda3/envs/CenterTrack/lib/python3.8/subprocess.py", line 415, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/home/xx/anaconda3/envs/CenterTrack/lib/python3.8/subprocess.py", line 516, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['git', 'describe']' returned non-zero exit status 128.

解决:

1)将logger.py中的下面几行注释了

    with open(file_name, 'wt') as opt_file:
      # opt_file.write('==> commit hash: {}\n'.format(
      #   subprocess.check_output(["git", "describe"])))
      opt_file.write('==> torch version: {}\n'.format(torch.__version__))
      opt_file.write('==> cudnn version: {}\n'.format(


2)将subprocess.py中415行中的check=True改为False。

影响:

当子进程返回非零退出码时,subprocess就不会引发CalledProcessError异常。相反,它将静默失败,并继续执行剩余的代码。你将需要自己检查返回码(通过subprocess.CompletedProcess.returncode)来确定子进程是否成功执行。

这可能有助于防止一些错误,如尝试运行不存在的命令或在非Git目录中运行git describe命令等。然而,这也可能使你错过一些重要的错误信息,因为你的代码可能在出现错误的情况下仍然继续执行。你需要根据你的特定需求来决定是否要设置check=False。

### 解决 YOLOv7 中 `subprocess.check_output` 获取最新 Git Tag 报错 当尝试通过 `subprocess.check_output` 来获取最新的 Git tag 时遇到错误 `"Command 'git tag' returned non-zero exit status 128"`,这通常意味着 Git 命令未能成功执行。此问题可能源于当前工作目录不正确或者仓库本身存在问题。 为了修正这个问题,在调用 Git 命令之前应确保设置正确的路径以便命令能在合适的上下文中被执行[^2]。具体来说,可以通过修改 Python 脚本中的相关部分来指定 Git 命令应该在哪一个目录下运行: ```python import os import subprocess def get_latest_tag(repo_path='.'): # 切换到目标仓库所在的绝对路径 abs_repo_path = os.path.abspath(repo_path) try: # 设置环境变量以改变默认的工作目录 env = dict(os.environ, GIT_WORK_TREE=abs_repo_path) output = subprocess.check_output( ['git', '-C', repo_path, 'describe', '--tags', '--abbrev=0'], stderr=subprocess.STDOUT, text=True, env=env ) latest_tag = output.strip() return latest_tag except subprocess.CalledProcessError as e: print(f"Failed to retrieve tags from repository at {repo_path}. Error message:\n{e.output}") raise ``` 上述代码片段展示了如何安全地处理潜在异常并提供有用的反馈信息给开发者。此外,还演示了怎样利用 `-C` 参数直接告诉 Git 更改操作位置而无需显式更改脚本所在的工作目录[^4]。 如果仍然存在权限或其他配置方面的问题,则建议先手动验证能否正常访问该远程库以及是否有未解决的冲突或损坏情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值