繁体   English   中英

在 windows 上使用子进程 python 3.8 从 git 克隆存储库时出错

[英]Error in cloning repo from git with subprocess python 3.8 on windows

我正在编写一个用于克隆 git 存储库的脚本。 但我有以下错误:

Traceback (most recent call last):
  File "script.py", line 313, in <module>
    analysis = Analyzer(repo, mfPath, qPath, ssPath, ssPropertiesPath)
  File "script.py", line 21, in __init__
    self._set_conf()
  File "script.py", line 26, in _set_conf
    self._configure_repo()
  File "script.py", line 98, in _configure_repo
    self._git_clone()
  File "script.py", line 73, in _git_clone
    git_process = subprocess.Popen(['git', 'clone', self.repo],
  File "C:\Users\Daisy\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\Daisy\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

这是我与第 73 行错误相关的代码

def _git_clone(self):
        print('\nCloning git repository...\n')
        git_process = subprocess.Popen(['git', 'clone', self.repo],
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.STDOUT)
        git_process.wait()

    def _git_checkout(self, commit_hash):
        git_process = subprocess.Popen(['git', 'checkout', commit_hash],
                                        cwd=self.projectPath,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.STDOUT)
        git_process.wait()

    def _get_commits(self):
        git_process = subprocess.Popen(['git', '--no-pager', 'log', '--pretty=%H'],
                                        cwd=self.projectPath,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.STDOUT)
        commits = git_process.communicate()[0][:-1].decode('utf-8').split('\n')
        self.projectCommits = commits[::-1]

我该如何解决? 谢谢

您可以使用shell=True尝试它,但在这种情况下,您需要将命令作为字符串而不是列表传递。 如下图所示

git_process = subprocess.Popen('git clone <repo_url>',
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.STDOUT, shell=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM