错误:没有名为Elasticsearch的模块

时间:2018-06-16 13:47:53

标签: python-3.x elasticsearch centos

我在Google Cloud(CentOS)上运行虚拟环境。

我已将es_v安装为我的虚拟环境,并按these instructions安装弹性搜索。

(es_v)$ which java
/usr/bin/java
(es_v)$ which python
/var/www/my_project/elastic/es_v/bin/python
(es_v)$ python --version
Python 3.6.5

我运行配置命令并运行服务:

sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service 
sudo systemctl start elasticsearch.service

当我尝试提取数据时( test.py ):

from datetime import datetime
from elasticsearch import Elasticsearch

es = Elasticsearch()

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
   }
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['result'])

res = es.get(index="test-index", doc_type='tweet', id=1)
print(res['_source'])

es.indices.refresh(index="test-index")

res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
    print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])

我明白了:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from elasticsearch import Elasticsearch
ModuleNotFoundError: No module named 'elasticsearch'

我尝试安装elasticsearch:

sudo pip install elasticsearch

它指向了目录:

Requirement already satisfied: elasticsearch in /usr/lib/python3.6/site-packages (6.2.0)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/lib/python3.6/site-packages (from elasticsearch) (1.22)

我应该包含/ usr / elasticsearch /和/ usr / elasticsearch / bin / somewhere吗?它似乎找不到它,因为我指的不是完整的路径。

任何关于我做错事的指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可能要检查Scrapy使用的python版本是否与pip安装到的python- [version_number]文件夹共享相同的版本。就我而言,我的Scrapy安装在python3.8上,并从/usr/local/lib/Python3.8获取依赖关系,但是在更新Python之后,我的提示指向/usr/local/lib/Python3.9。这就是为什么Scrapy找不到新添加的模块'elasticsearch'。

解决方案是在命令中指定版本,如下所示: python3.8 -m pip install elasticsearch