从github repo下载并从仓库中导入.py文件以供使用



我正在研究一个需要从开源git repo中启用一些实用程序功能的项目。目前,我在本地下载了实用程序,并在我的python代码中导入它:

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

但是,我想自动化此过程:

说,它可以从:https://github.com/spacenetchallenge/utilities.git

下载所需的实用程序

到给定的路径和运行代码时从该路径中导入。

谢谢!

有几种方法可以从Python下载Github的回购。这是一对夫妇,我确定还有更多:

1:外壳要git,或:2:从GitHub下载TGZ并解开

在第一种方法中,做这样的事情(未经测试(:

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

对于第二种方式(TGZ(,使用urllib.request(也未经测试(:

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

在这两种情况下,您都可能需要检查它尚未下载,因此并非每次都重新安装。当然,添加了很多错误检查。

最新更新