Ander
Ander

Reputation: 5644

ImportError when importing elasticsearch.helpers in Python

I am using a docker container with Ubuntu 16.04 Xenial with a Python 3.5.2 virtual environment. Every time I am trying to initialize the server uWSGI I am getting the following Python error:

  File "/env/lib/python3.5/site-packages/elasticsearch_dsl/__init__.py", line 5, in <module>
    from .search import Search
  File "/env/lib/python3.5/site-packages/elasticsearch_dsl/search.py", line 3, in <module>
    from elasticsearch.helpers import scan
ImportError: No module named 'elasticsearch.helpers'

I have been running the bash manually inside the docker container to see what is wrong with the module. By running pip list in the virtual environment I can tell that the packages are correctly installed:

# pip list
elasticsearch (2.1.0)
elasticsearch-dsl (0.0.8)

Running sys.path shows that the site packages folder was correctly added to the python path:

import sys
sys.path
[
  '', 
  '/env/lib/python35.zip', 
  '/env/lib/python3.5', 
  '/env/lib/python3.5/plat-x86_64-linux-gnu', 
  '/env/lib/python3.5/lib-dynload', 
  '/usr/lib/python3.5', 
  '/usr/lib/python3.5/plat-x86_64-linux-gnu', 
  '/env/lib/python3.5/site-packages'
]

There is a __init__.py file within the folder elasticsearch.helpers so that's not the problem. If I try the import from the Python console it will fail as well:

from elasticsearch.helpers import scan
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'elasticsearch.helpers'

But now if I go to the site-packages folder cd /env/lib/python3.5/site-packages and run python console from here, the import will work.

I don't really know why this is happening any help will be appreciated.

Upvotes: 1

Views: 2075

Answers (1)

Ander
Ander

Reputation: 5644

ouch... just a rookie mistake. One of the folders of my project was called elasticsearch and that was causing the issue.

Running the following command I found out that my app was simply loading the elasticsearch module from a different location.

import elasticsearch
import pprint, os


pprint.pprint(os.path.abspath(elasticsearch.__file__))
# /var/sites/my_app/elasticsearch/__init__.py

Upvotes: 4

Related Questions