我已经用 Python 编写了一些库以在我的项目中使用。我已将它们本地存储在我的系统上,也远程存储在 Github 上。现在每次我写一些代码时我都会使用sys.path.append()
一开始帮助从系统中的目录导入我的库。我想知道是否有办法直接从我的 Github 存储库导入这些文件
我的存储库的链接是这样的 -Quacpy https://github.com/biryani/Quacpy
如果您想使用必须安装的存储库,我不确定您希望如何在另一个 python 脚本中自动安装(以及如果安装失败该怎么办)。
但是,如果您只想使用另一个文件中的某些方法,您可以下载该文件,然后导入它:
import urllib2
def download(url):
filename = url.split('/')[-1]
print 'Downloading', filename
f = urllib2.urlopen(url)
data = f.read()
f.close()
with open(filename, 'w') as myfile:
myfile.write(data)
# get repository
download('https://raw.githubusercontent.com/biryani/Quacpy/master/auxfun.py')
# try to import something from it
from auxfun import qregnorm
q = qregnorm([0, 1, 2])
print 'Success! q =', q
也许您甚至可以下载整个 zip 文件,将其解压缩,然后导入文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)