简体   繁体   中英

Cloning a git repo from git bash using python script from command prompt failing. Why?

The git bash closes abruptly without cloning the repo. I'm unable to understand what's wrong here.

import os
import subprocess
    
parent_dir = r'C:\Users\user\Documents'
dir_name = 'Git_temp'
dir_path = os.path.join(parent_dir, dir_name)
    
os.mkdir(dir_path)
print("{0} created under {1}".format(dir_name, parent_dir))
os.chdir(dir_path)
print("Current Working Directory : {0}".format(os.getcwd()))
git_file = "C:\Program Files\Git\git-bash.exe"
git_clone_cmd = "git clone https://github.com/patankar-saransh/test_repo.git"
subprocess.Popen([git_file, git_clone_cmd])

If you do not want to use GitPython , then simply make sure git.exe is in your %PATH% .

Then your call would be:

import subprocess
process = subprocess.Popen(["git", "clone", "https://..."], stdout=subprocess.PIPE)
output = process.communicate()[0]

As seen here , with Python 2.7+, use check_output

import subprocess
output = subprocess.check_output(["git",  "clone", "https://..."]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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