Mathyou
Mathyou

Reputation: 653

Django no module named elasticsearch_dsl.connections

I'm trying to connect my Django model to the Elasticsearch server on local host but when I try

from elasticsearch_dsl.connections import connections

I get the error "ImportError: No module named elasticsearch_dsl.connections". When I use this same command in the Django shell, it works fine.

search.py

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import DocType, Text, Date, Boolean, Integer, Keyword, fields
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch

from .models import HomeGym, Country, Rating

connections.create_connection()

class HomeGymIndex(DocType):
    title = Text()
    price = fields.FloatField()
    tags = Keyword()

    city = Text()
    country = Text()

    rate = Integer()
    opusApproved = Boolean()

def bulk_indexing():
    HomeGymIndex.init()
    es = Elasticsearch()
    bulk(client=es, actions=(b.indexing() for b in HomeGym.objects.all().iterator()))

This leads to an ImportError on line 1. "No module named elasticsearch_dsl.connections" The same import statement works in the shell though.

I've already done a pip install of elasticsearch and elasticsearch-dsl inside my virtualenv.

Here is the file structure

my_website/
    elasticsearch/
        #elasticsearch files pulled from github
        elasticsearch-5.5.2-SNAPSHOT/
            #elasticsearch files
            bin/
                elasticsearch
    opus/
        manage.py
        homegymlistings/
            models.py
            search.py
            #other standard app files
        opus/
            #standard files for main django branch
    my_virtualenv/
        bin/
            activate

Why does my import statement only fail when called inside the search.py file located inside the homegymlistings app?

Upvotes: 3

Views: 8701

Answers (2)

Mathyou
Mathyou

Reputation: 653

Apparently I had to pip install elasticsearch and elasticsearch-dsl outside of the virtualenv. The error went away after that.

Upvotes: 2

vivek jha
vivek jha

Reputation: 354

Run this

pip install elasticsearch_dsl

Upvotes: 5

Related Questions