1

I am attempting to run the GitHub API client PyGithub in my Django project but I'm not sure how to import it. I have tried to move the file and change directories but nothing seems to work.

Below is my views.py file:

from django.shortcuts import render
from github import Github, GithubException

from .models import Drop

def index(request):
    return render(request, 'phrxns/index.html')

def drops(request):
    drops = Drop.objects.all()
    context = {'drops': drops}
    return render(request, 'phrxns/drops.html', context)

def github_client(request):
    search_result = {}
    if 'username' in request.GET:
        username = request.GET['username']
        client = Github()

        try:
            user = client.get_user(username)
            search_result['name'] = user.name
            search_result['login'] = user.login
            search_result['public_repos'] = user.public_repos
            search_result['success'] = True
        except GithubException as ge:
            search_result['message'] = ge.data['message']
            search_result['success'] = False

        rate_limit = client.get_rate_limit()
        search_result['rate'] = {
            'limit': rate_limit.rate.limit,
            'remaining': rate_limit.rate.remaining,
        }

    return render(request, 'core/github.html', {'search_result': search_result})

The server error I'm receiving is

File "/Library/Code/phrxn/phrxns/views.py", line 2, in <module>
    from github import Github, GithubException
ModuleNotFoundError: No module named 'github'
phast
  • 188
  • 1
  • 2
  • 12
  • 1
    Does this answer your question? [How to import a module given its name as string?](https://stackoverflow.com/questions/301134/how-to-import-a-module-given-its-name-as-string) – kboom Dec 28 '19 at 20:52

0 Answers0