elastic / elasticsearch-dsl-py

High level Python client for Elasticsearch
http://elasticsearch-dsl.readthedocs.org
Apache License 2.0
3.84k stars 802 forks source link

cannot import name 'ApiError' from 'elasticsearch.exceptions' #1854

Closed Woiea closed 3 months ago

Woiea commented 5 months ago
from elasticsearch import Elasticsearch, RequestsHttpConnection
from elasticsearch_dsl import Search, Q
Woiea commented 5 months ago

Name: elasticsearch Version: 8.14.0 Name: elasticsearch-dsl Version: 8.14.0

pquentin commented 5 months ago

Hello @Woiea, and thanks for your report. We're going to need more information to help you.

Woiea commented 5 months ago

I'm trying to run my code to access the elasticksearch database, and haven't used the code yet. Just import the libraries.

File /data/usr/utils/search_in_es.py:3
      1 from typing import List, Optional
      2 from elasticsearch import Elasticsearch, RequestsHttpConnection
----> 3 from elasticsearch_dsl import Search, Q
      4 OPS_ES = Elasticsearch(
   (...)
      9     timeout=30
     10 )

File /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch_dsl/__init__.py:21
     19 from .aggs import A
     20 from .analysis import analyzer, char_filter, normalizer, token_filter, tokenizer
---> 21 from .document import AsyncDocument, Document
     22 from .document_base import InnerDoc, MetaField
     23 from .exceptions import (
     24     ElasticsearchDslException,
     25     IllegalOperation,
     26     UnknownDslObject,
     27     ValidationException,
     28 )

File /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch_dsl/document.py:18
      1 #  Licensed to Elasticsearch B.V. under one or more contributor
      2 #  license agreements. See the NOTICE file distributed with
      3 #  this work for additional information regarding copyright
   (...)
     15 #  specific language governing permissions and limitations
     16 #  under the License.
---> 18 from elasticsearch_dsl._async.document import AsyncDocument  # noqa: F401
     19 from elasticsearch_dsl._sync.document import Document  # noqa: F401
     20 from elasticsearch_dsl.document_base import InnerDoc, MetaField

File /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch_dsl/_async/document.py:22
     18 import collections.abc
     20 from elasticsearch.exceptions import NotFoundError, RequestError
---> 22 from .._async.index import AsyncIndex
     23 from ..async_connections import get_connection
     24 from ..document_base import DocumentBase, DocumentMeta

File /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch_dsl/_async/index.py:22
     20 from ..index_base import IndexBase
     21 from .mapping import AsyncMapping
---> 22 from .search import AsyncSearch
     23 from .update_by_query import AsyncUpdateByQuery
     26 class AsyncIndexTemplate:

File /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch_dsl/_async/search.py:20
      1 #  Licensed to Elasticsearch B.V. under one or more contributor
      2 #  license agreements. See the NOTICE file distributed with
      3 #  this work for additional information regarding copyright
   (...)
     15 #  specific language governing permissions and limitations
     16 #  under the License.
     18 import contextlib
---> 20 from elasticsearch.exceptions import ApiError
     21 from elasticsearch.helpers import async_scan
     23 from ..async_connections import get_connection

ImportError: cannot import name 'ApiError' from 'elasticsearch.exceptions' (/data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch/exceptions.py)

RequestsHttpConnection doesn't cause any error until now.

python -m pip freeze:

elastic-transport==8.13.1
elasticsearch==8.14.0
elasticsearch-dsl==8.14.0
pquentin commented 5 months ago

The elasticsearch-py package in /data/usr/tools/miniconda/envs/pytorch/lib/python3.10/site-packages/elasticsearch/ is not version 8.14.0. With 8.14.0, import RequestsHttpConnection should fail, while import ApiError from exceptions should work:

>>> from elasticsearch import Elasticsearch, RequestsHttpConnection
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'RequestsHttpConnection' from 'elasticsearch' (/home/q/src/elasticsearch-py/elasticsearch/__init__.py)
>>> from elasticsearch.exceptions import ApiError
>>> 

To confirm this, you should add the following code at the top in search_in_es.py (before any other imports):

import elasticsearch
print(elasticsearch.__versionstr__)