Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Woiea opened this issue Jul 2, 2024 · 4 comments
Open

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

Woiea opened this issue Jul 2, 2024 · 4 comments
Labels
Area: Client Manually written code that fits in no other area Category: Not an issue

Comments

@Woiea
Copy link

Woiea commented Jul 2, 2024

from elasticsearch import Elasticsearch, RequestsHttpConnection
from elasticsearch_dsl import Search, Q
@Woiea
Copy link
Author

Woiea commented Jul 2, 2024

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

@pquentin
Copy link
Member

pquentin commented Jul 2, 2024

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

  • What are you trying to do? Did you use to have working code?
  • Can you please paste the full traceback and not just cannot import name 'ApiError' from 'elasticsearch.exceptions'?
  • RequestsHttpConnection was a class in elasticsearch-py 7.x, but it does not exist in elasticsearch-py 8.x. This should be the first error. Can you please paste the output of python -m pip freeze?

@Woiea
Copy link
Author

Woiea commented Jul 2, 2024

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
Copy link
Member

pquentin commented Jul 2, 2024

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

@pquentin pquentin added Area: Client Manually written code that fits in no other area Category: Not an issue labels Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Client Manually written code that fits in no other area Category: Not an issue
Projects
None yet
Development

No branches or pull requests

2 participants