Executing SPARQL queries with the wikibaseintegrator package on a local Wikibase instance

180 Views Asked by At

I'm attempting to run a SPARQL query using the Python package wikibaseintegrator (version 0.10.0).

The program is written as follows:

from wikibaseintegrator.wbi_config import config as wbi_config
from wikibaseintegrator import wbi_login, wbi_core

wbi_config['MEDIAWIKI_API_URL'] = 'http://localhost/database_name/api.php'
wbi_config['SPARQL_ENDPOINT_URL'] = 'http://localhost:8989/database_name/sparql'
wbi_config['WIKIBASE_URL'] = 'http://wikibase.svc'

temp_username = "placeholder_username"
temp_password = "placeholder_password"

def main():
    login_instance = login()
    
    sparql_str = """
        SELECT ?item ?itemLabel 
        WHERE 
        {
          ?item wdt:P98 wd:Q45.
          SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
        }
    """
    
    sparql_results = run_sparql_query(sparql_str)
    print(sparql_results)

def login(username=temp_username, password=temp_password):
    login_instance = wbi_login.Login(user=username, pwd=password)
    return login_instance
    
def run_sparql_query(sparql_str):
    sparql_results = wbi_core.ItemsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
    return sparql_results

#    MAIN
if __name__ == "__main__": main()

When I run this though, the error I get is:

Traceback (most recent call last):
  File "database_script.py", line 52, in <module>
    if __name__ == "__main__": main()
  File "database_script.py", line 40, in main
    sparql_results = run_sparql_query(sparql_str)
  File "database_script.py", line 48, in run_sparql_query
    sparql_results = wbi_core.ItemsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'ItemsEngine'

However, the documentation (https://github.com/LeMyst/WikibaseIntegrator) seems to imply that this is the correct way to format the query. Any help in diagnosing would be much appreciated!

EDIT 1: The documentation says it's in the ItemEngine (The method wbi_core.ItemEngine.execute_sparql_query()), but the program itself seems to show it being in the FuctionsEngine

I have tried all of these variations, with the error being the same:

$ python database_script.py
Traceback (most recent call last):
  File "database_script.py", line 52, in <module>
    if __name__ == "__main__": main()
  File "database_script.py", line 40, in main
    sparql_results = run_sparql_query(sparql_str)
  File "database_script.py", line 48, in run_sparql_query
    sparql_results = wbi_core.FuctionsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'FuctionsEngine'

$ python database_script.py
Traceback (most recent call last):
  File "database_script.py", line 52, in <module>
    if __name__ == "__main__": main()
  File "database_script.py", line 40, in main
    sparql_results = run_sparql_query(sparql_str)
  File "database_script.py", line 48, in run_sparql_query
    sparql_results = wbi_core.ItemEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: type object 'ItemEngine' has no attribute 'execute_sparql_query'

$ python database_script.py
Traceback (most recent call last):
  File "database_script.py", line 52, in <module>
    if __name__ == "__main__": main()
  File "database_script.py", line 40, in main
    sparql_results = run_sparql_query(sparql_str)
  File "database_script.py", line 48, in run_sparql_query
    sparql_results = wbi_core.FunctionEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'FunctionEngine'

EDIT 2: The larger issue seemed to be the lack of an install for the SPARQL service since I had gotten it running with WAMP64. I installed a Docker instance and it's been a decent amount easier out of the box (except the export of the WAMP64 version and import into the Docker instance).

0

There are 0 best solutions below