import pywikibot
from pywikibot import pagegenerators as pg
import re

site = bot.Site('commons', 'commons')

def category_exists(cat):
    Ensure a given category really exists on Commons.
    If a cache is provided the replies are cached to reduce the number of
    :param cat: category name (with or without "Category" prefix)
    :param cache: The cache in which to store the values
    :return: bool
    if not cat.lower().startswith('category:'):
        cat = 'Category:{0}'.format(cat)

    exists = pywikibot.Page(site, cat).exists()

    return exists

title = "Lundby nya kyrka"
if category_exists(title):
    cat = pywikibot.Page(site, "Category:" + title)
    for parent in cat.categories():
[[commons:Category:Lundby nya kyrka]]
Churches in Göteborg (urban area)
Churches in Göteborg Municipality
Churches in Sweden built in 1996
Churches in the Diocese of Gothenburg
Listed churches in Göteborg Municipality