Import python libraries from Github

I have written several libraries in Python for use in my project. I saved them locally on my system, and also remotely on Github. Now every time I write code, I first use sys.path.append() to help import my libraries from the directory on my system. I was wondering if, in any case, importing these files directly from my Github repository

Link to my repo - Quacpy

+7
python github sys
source share
4 answers

This is a bit like a wall, but it might work for you (if any of your libraries depend on each other, you will also have to change their import into githubimports !?):

 import requests def githubimport(user, repo, module): d = {} url = 'https://raw.githubusercontent.com/{}/{}/master/{}.py'.format(user, repo, module) r = requests.get(url).text exec(r, d) return d qoperator = githubimport('biryani', 'Quacpy', 'qoperator') 
+1
source share

If you want to use a repo that needs to be installed, I'm not sure how you want to automate the installation inside another python script (also what to do if the installation does not work).

However, if you just want to use some methods from another file, you can download this file and then import it:

 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 

Perhaps you can even download the entire zip code, unzip it, and then import the files.

+2
source share

Assuming you have a setup.py file, pip supports git based installation. See https://pip.pypa.io/en/latest/reference/pip_install.html#git for details

Spoiler: since you do not have setup.py, you will see the following error if you are currently trying to use 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 
+2
source share

This imports the entire repository as a module, 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 
+2
source share

All Articles