0

While integrating this snippet into a script, I wanted to change from :

import pywikibot
from pywikibot import pagegenerators

site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()

sparql = "SELECT ?item WHERE { ?item rdfs:label 'Google'@en }"
entities = pagegenerators.WikidataSPARQLPageGenerator(sparql, site=repo)

to:

import pywikibot

site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()

sparql = "SELECT ?item WHERE { ?item rdfs:label 'Google'@en }"
entities = pywikibot.pagegenerators.WikidataSPARQLPageGenerator(sparql, site=repo)

(i.e trying to merge the imports into a single one) but it gives me an error:

AttributeError: module 'pywikibot' has no attribute 'pagegenerators'

(The same error on my desktop and on a Jupyter notebook in the cloud)

What am I missing ?

Jona
  • 1,218
  • 1
  • 10
  • 20
  • 2
    pretty-much-duplicate: [Why does this AttributeError in python occur?](//stackoverflow.com/q/8696322) – Aran-Fey Mar 31 '19 at 20:59
  • @Aran-Fey Thanks, I guessed it has been already asked but was not able to find it. – Jona Mar 31 '19 at 21:10
  • 1
    **pagegenerators** is indeed a submodule, but that isn't the whole story. It is up to the pywikibot module's **__init__.py** file to decide what gets imported directly into your default namespace. Many top level modules will import their submodules into the global namespace by doing **import – CryptoFool Mar 31 '19 at 21:19

1 Answers1

2

Pagegenerators is not an attribute, it's a module inside pywikibot: https://m.mediawiki.org/wiki/Manual:Pywikibot/pagegenerators.py

Submodules are not imported automatically in this particular module, that's why you have to write from pywikibot import pagegenerators.

  • OK, and as said in the duplicate : "Submodules don't automatically get imported when you just `import X`". – Jona Mar 31 '19 at 21:12
  • ...and as I describe in my comment, it's not quite this simple in the general case. But I see that @HeavenlyRaven may be talking about just this module specifically, in which case he is correct. – CryptoFool Mar 31 '19 at 21:24