Search code examples
pythongithubsys

Importing python libraries from Github


I have written some libraries in Python for use in my project. I have stored them locally on my system and also remotely on Github. Now every time I write some code I use sys.path.append() in the beginning to help import my libraries from the directory in my system. I was wondering that if there is anyway to import these files directly from my Github repository

The link to my repo is this - Quacpy


Solution

  • This feels a bit off the wall but might work for you (if any of your libraries depend on each other, you'll have to change those imports to githubimports too!?):

    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')