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
    lookups.
    :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)
    print(cat)
    for parent in cat.categories():
        print(parent.title(withNamespace=False))
[[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
Kyrkbyn
Listed churches in Göteborg Municipality