简体   繁体   English

“从 a 导入 b”不起作用,但我可以导入 a 然后使用 ab

[英]"from a import b" not working, but I can import a and then use a.b

Overview概述

from elasticsearch import Elasticsearch does not work. from elasticsearch import Elasticsearch不起作用

import elasticsearch
e = elasticsearch.Elasticsearch(...)

does work.确实有效。

Deets迪茨

I am trying to use a simple Elasticsearch client in python using AWS (ssh'd on an Amazon linux e3 machine).我正在尝试使用 AWS 在 python 中使用一个简单的 Elasticsearch 客户端(在 Amazon linux e3 机器上使用 ssh)。 The code I am copying is here .我正在复制的代码在这里 I am unable to import the Elasticsearch class as described in the guide.我无法按照指南中的说明导入 Elasticsearch 类。

Using from elasticsearch import Elasticsearch gives me the error: ImportError: cannot import name 'Elasticsearch' .使用from elasticsearch import Elasticsearch给了我错误: ImportError: cannot import name 'Elasticsearch'

I opened the python3 cli to check it out.我打开了python3 cli来检查它。 If I type from elasticsearch import E and tab-complete, I get the following suggestions: EOFError( Ellipsis EnvironmentError( Exception( . However from elasticsearch import Ellipsis gives me ImportError: cannot import name 'Ellipsis' .如果我from elasticsearch import E和 tab-complete 输入,我会得到以下建议: EOFError( Ellipsis EnvironmentError( Exception( 。但是from elasticsearch import Ellipsis给了我ImportError: cannot import name 'Ellipsis'

If I type import elasticsearch , then on the next line elasticsearch.如果我输入import elasticsearch ,那么在下一行elasticsearch. and hit tab to autocomplete, I get the full range that I would expect ( Elasticsearch( , RequestsHttpConnection( , etc.).并点击选项卡自动完成,我得到了我期望的全部范围( Elasticsearch(RequestsHttpConnection(等)。

I assume that this has something to do with the way/where it is installed.我认为这与它的安装方式/位置有关。

I used pip3 install elasticsearch --user to install it originally.我最初使用pip3 install elasticsearch --user安装它。 I uninstalled it ( pip3 uninstall elasticsearch ) and returned to the python cli.我卸载了它( pip3 uninstall elasticsearch )并返回到 python cli。 from elasticsearch import E still gives me EOFError( Ellipsis EnvironmentError( Exception( on the tab-complete, but from elasticsearch import Ellipsis now returns ModuleNotFoundError: No module named 'elasticsearch' , as does just import elasticsearch . from elasticsearch import E仍然给我EOFError( Ellipsis EnvironmentError( Exception( on the tab-complete, but from elasticsearch import Ellipsis ModuleNotFoundError: No module named 'elasticsearch' from elasticsearch import Ellipsis ModuleNotFoundError: No module named 'elasticsearch'现在返回ModuleNotFoundError: No module named 'elasticsearch' ,就像import elasticsearch

Not really quite sure what is up.不太确定发生了什么。 I did not tag this as elasticsearch because it might be a user error :P我没有将其标记为elasticsearch,因为它可能是用户错误:P

  • which python3 : /usr/bin/python3 which python3 :/usr/bin/python3
  • which pip3 : ~/.local/bin/pip3 which pip3 : ~/.local/bin/pip3
  • pip3 --version : pip 18.1 from /home/ec2-user/.local/lib/python3.6/site-packages/pip (python 3.6) pip3 --version :来自 /home/ec2-user/.local/lib/python3.6/site-packages/pip (python 3.6) 的 pip 18.1

My problem was that I had named my file the same thing as the module I was trying to import from - elasticsearch.py .我的问题是我将我的文件命名为与我试图从中导入的模块相同的东西 - elasticsearch.py As user2357112 states, I became hung up on the incorrect autocomplete.正如 user2357112 所说,我被不正确的自动完成所困扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM