Django没有名为elasticsearch_dsl.connections的模块

时间:2017-09-12 00:19:58

标签: python django elasticsearch python-import importerror

我尝试将我的Django模型连接到本地主机上的Elasticsearch服务器,但是当我尝试

from elasticsearch_dsl.connections import connections

我收到错误" ImportError:没有名为elasticsearch_dsl.connections"的模块。 当我在Django shell中使用相同的命令时,它工作正常。

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()))

这导致第1行出现ImportError。"没有名为elasticsearch_dsl.connections的模块" 但是,相同的import语句在shell中有效。

我已经在virtualenv中完成了elasticsearch和elasticsearch-dsl的pip安装。

这是文件结构

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

为什么我的import语句只在homegymlistings app内的search.py​​文件中调用时失败?

2 个答案:

答案 0 :(得分:2)

运行此

pip install elasticsearch_dsl

答案 1 :(得分:1)

显然我不得不在virtualenv上安装elasticsearch和elasticsearch-dsl 。之后错误就消失了。