python - Django 没有名为 elasticsearch_dsl.connections 的模块

标签 python django elasticsearch python-import importerror

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

from elasticsearch_dsl.connections import connections

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

搜索.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。“No module named 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

为什么只有在位于 homegymlistings 应用程序内的 search.py​​ 文件中调用我的导入语句时才会失败?

最佳答案

运行这个

pip install elasticsearch_dsl

关于python - Django 没有名为 elasticsearch_dsl.connections 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166092/

相关文章:

elasticsearch - 未发现 Elastic Search Master 异常

elasticsearch - 在多个字段中过滤高级Rest Java Elasticsearch Client

python - Python 多参数返回语法糖吗?

python - 如何估计具有非均匀平移/旋转的图像之间的亚像素偏移

python - 使用正则表达式解析 winerror.h 和 ntstatus.h 状态/定义

python - 从 Django 登录中检索 SQL 条目

python - 无法读取图片列表 cv2.imread()

python - 尝试在 mac os leopard 上安装 lxml

python - Django模板如何修复错误 "Could not parse the remainder: ' |' from ' |' "

curl - 将多个自定义分析器设置为 Elasticsearch 中的单个字段