从Github导入python库
我已经用Python编写了一些库,以便在我的项目中使用。我已经将它们本地存储在我的系统上,也远程存储在Github上。现在,每次编写代码时,我都会在开始时使用从Github导入python库,python,github,sys,Python,Github,Sys,我已经用Python编写了一些库,以便在我的项目中使用。我已经将它们本地存储在我的系统上,也远程存储在Github上。现在,每次编写代码时,我都会在开始时使用sys.path.append()来帮助从系统中的目录导入库。我想知道是否有任何方法可以直接从我的Github存储库导入这些文件 指向my repo的链接是-这感觉有点离谱,但可能适合您(如果您的任何库相互依赖,您也必须将这些导入更改为githubimports!?): 如果您想使用必须安装的repo,我不确定您希望如何在另一个python
sys.path.append()
来帮助从系统中的目录导入库。我想知道是否有任何方法可以直接从我的Github存储库导入这些文件
指向my repo的链接是-这感觉有点离谱,但可能适合您(如果您的任何库相互依赖,您也必须将这些导入更改为githubimports!?):
如果您想使用必须安装的repo,我不确定您希望如何在另一个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文件,解压后导入文件。假设你有一个有效的setup.py文件,
pip
支持基于git的安装。有关详细信息,请参阅
扰流板:由于您没有setup.py文件,如果您当前尝试使用pip,您将看到以下错误:
pip install -e git+https://github.com/biryani/Quacpy.git#egg=quacpy
Obtaining quacpy from git+https://github.com/biryani/Quacpy.git#egg=quacpy
Cloning https://github.com/biryani/Quacpy.git to /.../quacpy
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 18, in <module>
IOError: [Errno 2] No such file or directory: '/.../quacpy/setup.py'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /.../quacpy
pip安装-e git+https://github.com/biryani/Quacpy.git#egg=quacpy
从git获得quacpy+https://github.com/biryani/Quacpy.git#egg=quacpy
克隆https://github.com/biryani/Quacpy.git 至/../quacpy
从命令python setup.py egg_info完成输出:
回溯(最近一次呼叫最后一次):
文件“”,第18行,在
IOError:[Errno 2]没有这样的文件或目录:'/…/quacpy/setup.py'
----------------------------------------
命令“python setup.py egg_info”在/../quacpy中失败,错误代码为1
这将整个存储库作为模块导入,Python 3:
import sys
import urllib.request # python 3
import zipfile
import os
REPOSITORY_ZIP_URL = 'https://github.com/biryani/Quacpy/archive/master.zip'
filename, headers = urllib.request.urlretrieve(REPOSITORY_ZIP_URL)
zip = zipfile.ZipFile(filename)
directory = filename + '_dir'
zip.extractall(directory)
module_directory_from_zip = os.listdir(directory)[0]
module_directory = 'Quacpy'
os.rename(os.path.join(directory, module_directory_from_zip),
os.path.join(directory, module_directory))
sys.path.append(directory)
import Quacpy
我在Colab中使用的一种解决方法是将文件复制到Colab的文件夹中,然后将其导入Python的解释器中。对于您的库,这将是:
!wget -q https://github.com/biryani/Quacpy/__init__.py -O __init__.py > txt.log
from __init__ import *
应该有办法下载整个repo,必要时解压,然后导入。如果他们有
setup.py
您可以pip安装…
直接从GitHub repo下载感谢您的回答。但我并不打算安装该模块,而是每次运行代码时都从中远程导入一些文件。不过,你的方法是明智的。谢谢!这正是我想要的。事实并非如此简单。如果一个答案正是你所要寻找的,你应该考虑把它变成可接受的答案;
!wget -q https://github.com/biryani/Quacpy/__init__.py -O __init__.py > txt.log
from __init__ import *