我刚刚使用pip install elasticsearch安装了正确的软件包,但我的.py脚本找不到它。
我现在有这个:
ls /Library/Python/2.7/site-packages
README pip-1.5.6-py2.7.egg urllib3-1.8.3-py2.7.egg-info virtualenv.py virtualenv_support
easy-install.pth urllib3 virtualenv-1.11.6.dist-info virtualenv.pyc
ls /usr/local/lib/python2.7/site-packages /
easy-install.pth elasticsearch-1.0.0.dist-info setuptools-4.0.1-py2.7.egg sitecustomize.py
elasticsearch pip-1.5.6-py2.7.egg setuptools.pth sitecustomize.pyc
现在我运行我的脚本myelastic.py:
import sys
print sys.path
from elasticsearch import Elasticsearch
es = Elasticsearch()
我有这个:
['/Users/tati/Desktop/python', '/Applications/MAMP/Library/lib/python27.zip', '/Applications/MAMP/Library/lib/python2.7', '/Applications/MAMP/Library/lib/python2.7/plat-darwin', '/Applications/MAMP/Library/lib/python2.7/plat-mac', '/Applications/MAMP/Library/lib/python2.7/plat-mac/lib-scriptpackages', '/Applications/MAMP/Library/lib/python2.7/lib-tk', '/Applications/MAMP/Library/lib/python2.7/lib-old', '/Applications/MAMP/Library/lib/python2.7/lib-dynload', '/Applications/MAMP/Library/lib/python2.7/site-packages']
Traceback (most recent call last):
File "myelastic.py", line 5, in <module>
from elasticsearch import Elasticsearch
ImportError: No module named elasticsearch
这是我第一次使用virtualenv,但我不确定如何解决这个问题,谢谢!
答案 0 :(得分:13)
您已将文件命名为与模块相同的名称:
File "/Users/tati/Desktop/python/elasticsearch.py"
。
正在隐藏模块名称,因此您要从文件导入而不是弹性搜索模块。只需将.py
重命名为elasticsearch.py
以外的其他内容。
答案 1 :(得分:1)
我有同样的问题。我通过添加到我的.bash_profile来修复它:
export PYTHONPATH=/Library/Python/2.7/site-packages