简体   繁体   中英

Download from github repo and import .py file from the repo for usage

I'm working on a project that needs some utility functions from a open-source git repo. Currently, I download the utilities function locally and import it in my python code like this:

path_to_spacenet_utils = 'path/to/my/local/satellite_project/utilities'
sys.path.insert(0,path_to_spacenet_utils)
from spacenetutilities import geoTools as gT

However, I want to automate this process:

say, it can download the needed utilities from: https://github.com/SpaceNetChallenge/utilities.git

to a given path and import from that path when running the code.

Thank you!

There are several ways you could download a repo from github from python. Here's a couple, and I'm sure there are more:

1: shell out to git, or: 2: download a tgz from github and unpack it

For the first way, do something like this (untested):

import os, subprocess
os.chdir('some dir')
url = `https://github.com/SpaceNetChallenge/utilities.git`
subprocess.call('git clone "{}" spacenetV3'.format(url))

For the second way (tgz), use urllib.request (also untested):

import urllib.request, subprocess
url = 'https://github.com/SpaceNetChallenge/utilities/archive/spacenetV3.zip'  
urllib.request.urlretrieve(url, 'spacenetV3.zip')  
subprocess.call('unzip spacenetV3.zip')

In both cases, you'll probably want to check that it's not already downloaded, so it doesn't re-install every time. And of course add lots of error checking.

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