Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python not finding elasticsearch package

I just installed with pip install elasticsearch the right package, but is not being found by my .py script.

I have this right now:

ls /Library/Python/2.7/site-packages

README                       pip-1.5.6-py2.7.egg          urllib3-1.8.3-py2.7.egg-info virtualenv.py                virtualenv_support
easy-install.pth             urllib3                      virtualenv-1.11.6.dist-info  virtualenv.pyc

ls /usr/local/lib/python2.7/site-packages/

easy-install.pth              elasticsearch-1.0.0.dist-info setuptools-4.0.1-py2.7.egg    sitecustomize.py
elasticsearch                 pip-1.5.6-py2.7.egg           setuptools.pth                sitecustomize.pyc

Now when I run my script myelastic.py:

import sys
print sys.path

from elasticsearch import Elasticsearch
es = Elasticsearch()

I have this:

['/Users/tati/Desktop/python', '/Applications/MAMP/Library/lib/python27.zip', '/Applications/MAMP/Library/lib/python2.7', '/Applications/MAMP/Library/lib/python2.7/plat-darwin', '/Applications/MAMP/Library/lib/python2.7/plat-mac', '/Applications/MAMP/Library/lib/python2.7/plat-mac/lib-scriptpackages', '/Applications/MAMP/Library/lib/python2.7/lib-tk', '/Applications/MAMP/Library/lib/python2.7/lib-old', '/Applications/MAMP/Library/lib/python2.7/lib-dynload', '/Applications/MAMP/Library/lib/python2.7/site-packages']
Traceback (most recent call last):
  File "myelastic.py", line 5, in <module>
    from elasticsearch import Elasticsearch
ImportError: No module named elasticsearch

It's the first time I work with virtualenv, but I'm not sure how to work around this issue, thanks!

like image 272
Francisco Albert Avatar asked Jun 29 '14 17:06

Francisco Albert


People also ask

What is Elasticsearch DSL?

Elasticsearch DSL is a high-level library whose aim is to help with writing and running queries against Elasticsearch. It is built on top of the official low-level client ( elasticsearch-py ). It provides a more convenient and idiomatic way to write and manipulate queries.

How to use Elasticsearch with Python?

Still, you may use a Python library for ElasticSearch to focus on your main tasks instead of worrying about how to create requests. Install it via pip and then you can access it in your Python programs.

Is there a low-level client for Elasticsearch?

Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in Python; because of this it tries to be opinion-free and very extendable. Install the elasticsearch package with pip:

What if I have multiple versions of Elasticsearch installed?

If you have a need to have multiple versions installed at the same time older versions are also released as elasticsearch2 and elasticsearch5. Documentation for the client is available on elastic.co and Read the Docs.

What is the best alternative to Elasticsearch-Py?

For a more high level client library with more limited scope, have a look at elasticsearch-dsl - a more pythonic library sitting on top of elasticsearch-py.


2 Answers

You have called your file the same name as the module:

File "/Users/tati/Desktop/python/elasticsearch.py".

It is shadowing the module name so you are importing from your file not the elasticsearch module. Just rename your .py to something other than elasticsearch.py.

like image 118
Padraic Cunningham Avatar answered Oct 08 '22 11:10

Padraic Cunningham


I had the same issue. I fixed it by adding to my .bash_profile :

export PYTHONPATH=/Library/Python/2.7/site-packages
like image 44
Bruno Gallien Avatar answered Oct 08 '22 12:10

Bruno Gallien