[Tutorial] PyGithub #1

This isn’t the best way to do these things, it’s just the way I know how to do them.

PyGithub is a Python library used to access Github.

Starting off you need to import if not the library at least some things:

from github import Github

Then you need to create a github object:

g = Github(token)

The token you get from a Github account (under settings):


Now you have a github object, let’s get the organizations in this user’s account.

This part is cool, you organize all the organizations in the github object in a dictionary with the names of the organizations as the keys.

organizations = {organization.name: organization for organization in g.get_user().get_orgs()}

organization.name: sets the key to the organization name (a property in organizations), and then associates it with a specific organization in g’s organizations. It will cycle through all of the organizations, building a dictionary as it does so.

get_orgs() will give you all the organizations and the syntax “organization for organization” will give you all the organizations in the user’s account, basically it’s saying there’s a list of organizations, and get each organization out one at a time.

To get a specific organization, all you need to do is input the key (organization name) into the dictionary:

org = organizations[“NameOfOrganization”]

To get the repositories is the same method.

repos = {repo.name: repo for repo in org.get_repos()}

It’s the same structure as before. get_repos() returns all the repos in the organization (org) and the repos are organized into a dictionary with the name of the repos as the keys.

It would also be helpful to get a list of repositories for future use, and for that we use list comprehensions:

list_repos = [y for y in org.get_repos()]

It’s like the dictionary above, y for y in org.get_repos() basically means take each element y in the pool of y’s that returns from the get_repos() command.

That’ll be useful later on.

Now, to make a dictionary of teams:

teams = {team.name: team for team in org.get_teams()}

Teams are organized under Organizations, so to get all the Teams we have to get an Organization and then dump the teams through get_teams().

Finally, we could use a dictionary of members in an organization. We can add members to teams, but I think they have to be a member of the organization first.

members_org = {member.login: member for member in org.get_members()}

Now, if we want to add a member to a team:

def add_member_to_team(team, member_name):

members = {member.login: member for member in team.get_members()}

if (member_name.login not in members.keys()):


I wrote this function up above to try and encapsulate it.

members = {member.login: member for member in team.get_members()} gives us a dictionary of members in the team.

if (member_name.login not in members.keys()): checks to see if the member is already in the team.

team.add_to_members(member_name) this adds the member to the team.

This function requires a team (teams[“TeamA”]) and a member (members_org[“ExampleGithubUser”]).

Note, you can’t just put in a user login to add a character to a team, you have to send in a member from the members dictionary you build from the organization dictionary.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s