2

I am trying to learn SPARQL and to do that I need to execute queries against a local ttl file. I have researched this and every response says to use rdflib and then run a query against that.

Here is the example ttl file I am using

# filename: ex002.ttl

@prefix ab: <http://learningsparql.com/ns/addressbook#> .

ab:richard ab:homeTel "(229) 276-5135" . 
ab:richard ab:email   "richard49@hotmail.com" . 

ab:cindy ab:homeTel "(245) 646-5488" . 
ab:cindy ab:email   "cindym@gmail.com" . 

ab:craig ab:homeTel "(194) 966-1505" . 
ab:craig ab:email   "craigellis@yahoo.com" . 
ab:craig ab:email   "c.ellis@usairwaysgroup.com" .

Now the python code that is supposed to work is the following

filename = "C:/DataStuff/SemanticOntology/LearningSPARQLExamples/ex002.ttl" 
import rdflib
g = rdflib.Graph()

result = g.parse(filename, format='ttl')
print(result)
query = """
SELECT * WHERE {
        ?s ?p ?o .
}
"""

g.query(query)
for stmt in g:
    print(stmt)

Unfortunately whenever it gets to g.query(query) I get ModuleNotFoundError: No module named 'rdfextras'

Okay - no problem. So I go to conda-forge to install it, and it says that package is not available from current channels.

Okay, weird. So I do some research and discover https://github.com/RDFLib/rdfextras which says it is discontinued and it is no longer required for rdflib >=4. So I check my version of rdflib and it is 4.2.2. So obviously something is wrong, since it shouldn't require rdfextras.

So two questions -

  1. Why is it doing this if my version of rdflib >=4?
  2. Ultimately - this is all beside the point. I don't care about the means, so how do I use python to run SPARQL queries against local ttl files (in a way that doesn't require admin rights on my computer)

Thanks in advance.

TallTed
  • 9,069
  • 2
  • 22
  • 37
Dano13
  • 149
  • 9
  • *"so how do I use python to run SPARQL queries against local ttl files"* - with `rdflib` - this is by far the most common library for RDF/SPARQL written in Python. – UninformedUser Sep 11 '19 at 18:59
  • 1
    And I can't reproduce your problem. I did `pip install rdflib` and then simply used it. maybe you have multiple versions of the package on your system? Do you use `virtualenv` or the like? How do you run your code? From an IDE or via commandline? – UninformedUser Sep 11 '19 at 19:02
  • To install libraries I use a command prompt and then enter conda install -c conda-forge rdflib. To actually run the code I use spyder with anaconda. – Dano13 Sep 11 '19 at 19:15

1 Answers1

1

I apologize - I just got home and re-ran the query and now it seems to be working. I have no idea what changed between now and about 2 hours ago

Dano13
  • 149
  • 9
  • Now you should accept your own answer, or simply delete your question, as it seems to have been a no-op... – TallTed Sep 12 '19 at 15:43
  • Voting up your answer is not the same as [accepting](https://stackoverflow.com/help/self-answer) it... – TallTed Sep 13 '19 at 13:47
  • I agree - I should delete this post, but I don't see any way to do that. At best I can "vote" to delete it. So what is the etiquette here? – Dano13 Sep 13 '19 at 16:02
  • To delete the question, you'll need to first downvote (or delete) your answer. Then you should see a link that lets you delete the question, [as described here](https://stackoverflow.com/help/deleted-questions). – TallTed Sep 13 '19 at 19:36