GitHub Examples

Examples using the GitHub object.


I’ll just make some basic assumptions for the examples on this page. First, let’s assume that all you ever import from is login and GitHub and that you have already received your GitHub object g. That might look like this:

from github3 import login, GitHub
from getpass import getpass, getuser
import sys
    import readline
except ImportError:

    user = input('GitHub username: ') or getuser()
except KeyboardInterrupt:
    user = getuser()

password = getpass('GitHub password for {0}: '.format(user))

# Obviously you could also prompt for an OAuth token
if not (user and password):
    print("Cowardly refusing to login without a username and password.")

g = login(user, password)

So anywhere you see g used, you can safely assume that it is an instance where a user has authenticated already.

For the cases where we do not need an authenticated user, or where we are trying to demonstrate the differences between the two, I will use anon. anon could be instantiated like so:

anon = GitHub()

Also let’s define the following constants:

sigma = 'sigmavirus24'
github3 = ''
todopy = 'Todo.txt-python'
kr = 'kennethreitz'
requests = 'requests'

We may not need all of them, but they’ll be useful

Adding a new key to your account

    path = input('Path to key: ')
except KeyboardInterrupt:
    path = ''

    name = input('Key name: ')
except KeyboardInterrupt:
    name = ''

if not (path and name):  # Equivalent to not path or not name
    print("Cannot create a new key without a path or name")

with open(path, 'r') as key_file:
    key = g.create_key(name, key_file)
    if key:
        print('Key {0} created.'.format(key.title))
        print('Key addition failed.')

Deleting the key we just created

Assuming we still have key from the previous example:

if g.delete_key(
    print("Successfully deleted key {0}".format(

There would actually be an easier way of doing this, however, if we do have the key object that we created:

if key.delete():
    print("Successfully deleted key {0}".format(

Creating a new repository

repo = {}
keys = ['name', 'description', 'homepage', 'private', 'has_issues',
    'has_wiki', 'has_downloads']

for key in keys:
        repo[key] = input(key + ': ')
    except KeyboardInterrupt:

r = None
if repo.get('name'):
    r = g.create_repository(repo.pop('name'), **repo)

if r:
    print("Created {0} successfully.".format(

Create a commit to change an existing file

repo.file_contents('/').update('commit message', 'file content'.encode('utf-8'))

Follow another user on GitHub

I’m cheating here and using most of the follow functions in one example

if not g.is_following(sigma):

if not g.is_subscribed(sigma, github3py):
    g.subscribe(sigma, github3py)

if g.is_subscribed(sigma, todopy):
    g.unsubscribe(sigma, todopy)

for follower in g.iter_followers():
    print("{0} is following me.".format(follower.login))

for followee in g.iter_following():
    print("I am following {0}.".format(followee.login))

if g.is_following(sigma):

Changing your user information

Note that you can not change your login name via the API.

new_name = 'J. Smith'
blog = ''
company = 'Vandelay Industries'
bio = """# J. Smith

A simple man working at a latex factory

if g.update_user(new_name, blog, company, bio=bio):
    print('Profile updated.')

This is the same as:

me = # or me = g.user(your_user_name)
if me.update(new_name, blog, company, bio=bio):
    print('Profile updated.')