[英]Download from github repo and import .py file from the repo for usage
我正在开发一个项目,该项目需要来自开源 git 存储库的一些实用功能。 目前,我在本地下载实用程序函数并将其导入到我的 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: shell out to git, or: 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')
在这两种情况下,您可能都需要检查它是否尚未下载,因此不会每次都重新安装。 当然,还要添加大量的错误检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.