I’ve been trying to get this open source document repository system Invenio installed for the boss but today I ran into another problem:
Error: No module named solr
It turns out it wasn’t a problem peculiar to Invenio. Any Python script anywhere trying to
import solr was getting this error. Yet once again, there’s an easy solution.
Invenio’s Full-Text Search
You may have come across this error through a different means, but I ran into it while attempting to install and configure Invenio. One of the cool features of this document repository system is its ability to support full-text search.
But Invenio doesn’t do the searching itself. It hooks up with Apache Solr to handle the task.
Python and Solr
Invenio is just a collection of Python scripts. So when Invenio tries to
import solr in bibindex_engine.py, it’s not Invenio that’s trying to find Solr — it’s Python.
I discovered that this was not an Invenio issue when I opened up an interactive Python shell. I tried to
import solr from the shell and got the same error: no module named solr.
After looking around for a while, I finally figured out that though Python and Solr where both installed, Solr no longer comes packaged with a client for Python to access it.
All you need to do to fix this problem is install a Python Solr client:
$ sudo easy_install solrpy
Searching for solrpy
Best match: solrpy 0.9.5
Running solrpy-0.9.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-5th84f/solrpy-0.9.5/egg-dist-tmp-fINTfU
warning: no previously-included files matching '*.pyo' found anywhere in distribution
zip_safe flag not set; analyzing archive contents...
Adding solrpy 0.9.5 to easy-install.pth file
Processing dependencies for solrpy
Finished processing dependencies for solrpy
And that’s it. Your Python can now
import solr like a pro.