Gitlab API and Python

To start, create a config file with your Gitlab personal token:

vi ~/.python-gitlab.cfg

With contents similar to below:

 default = gitlab
 ssl_verify = true
 timeout = 5
 url =
 private_token = 1234ABCD1234ABCD
 api_version = 4

next create a directory and a python virtualenv:

mkdir ./code/python/test
python3 -m venv env
source env/bin/activate
pip3 install requests python-gitlab

Check which python binary we are using:

which python

Should be similar to:


And finally use the module in a Python script. In the example below I am fetching a list of all my groups. Filtering that for those that include the word ‘foobar’ then printing out each project in the groups with the shared_gitlab_runner attribute. I think pick an example project and change that setting to False:

import gitlab

gl = gitlab.Gitlab.from_config('gitlab', ['/Users/me/.python-gitlab.cfg'])

all_groups = gl.groups.list(all=True)

for group in all_groups:
    if "foobar" in str(group.full_name):
        projects = group.projects.list()

        for project in projects:

project = gl.projects.get(project_id)
project.shared_runners_enabled = False

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>