So you want to clone a git repo in Python3? I recently came across a case where I wanted to programmatically edit and update a Github repo and what better language to use than Python? My first go at the problem had me thinking of .bat files since I come from a Windows background. Therefore I had expected to have to run my Python script, exit and clone the repo, then continue on with the execution of the Python script. This would have complicated the workflow and I would have had to write a shell script to coordinate the python->shell->python maneuver.
Enter Stage Left subprocess.call()
I continued digging and eventually came across a very nifty module named subprocess which allows us to execute bash commands while still running through our Python script. It’ll look like the following.
import subprocess # Make sure you have git installed subprocess.call('git clone https://github.com/cvcaban/firstButton.git', shell = True) # Now you can edit the files that were downloaded or manipulate the info how you see fit
First thing is first. Make sure you have git installed on the machine you’re using. I don’t believe we get git out of the box on most machines. That’ll be manual
It’s succinct and clean. No need for any additional bash files. You can stay in your preferred language and not stress about synthesizing that coordinating file. Even better, if you’re using something like Apache Airflow, you can’t continue on with your typical scheduling instead of having to introduce CRON or edit your workflow to get that scheduled. Check out one of our other posts some something more meaty.