Python Pyhive 模块无法导入名称配置单元

Python Pyhive module cannot import name hive

我想使用 pyhive 将 Python 连接到配置单元。我正在使用下面的 python 脚本在我的本地执行。

#!/usr/bin/env python
# coding: utf-8
from pyhive import hive
from TCLIService.ttypes import TOperationState
def mysql_connect(host, port, username):
 conn = hive.Connection(host=host, port=port, username=username)
 return conn.cursor()

cursor = mysql_connect("localhost", 50070, "hduser")
cursor.execute("show databases")
print_log(cursor)

我在 /usr/local/lib/python2.7/dist-packages 位置安装了 pyhive,但我最终得到以下输出

vaibhav@vaibhav-Lenovo-G570:~/Desktop/Python/Automation$ ./pyhive_test.py
Traceback (most recent call last):
  File "./pyhive_test.py", line 9, in <module>
    cursor = mysql_connect("localhost", 50070, "hduser")
  File "./pyhive_test.py", line 6, in mysql_connect
    conn = hive.Connection(host=host, port=port, username=username)
  File "/usr/local/lib/python2.7/dist-packages/pyhive/hive.py", line 131, in __init__
    self._transport.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/__init__.py", line 80, in open
    status, payload = self._recv_sasl_message()
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/__init__.py", line 101, in _recv_sasl_message
    payload = read_all_compat(self._trans, length)
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/six.py", line 31, in <lambda>
    read_all_compat = lambda trans, sz: trans.readAll(sz)
  File "/home/vaibhav/.local/lib/python2.7/site-packages/thrift/transport/TTransport.py", line 60, in readAll
    chunk = self.read(sz - have)
  File "/home/vaibhav/.local/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 132, in read
    message='TSocket read 0 bytes')
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

编辑 1. 文件名由 Pyhive 改为 pyhive_test

  1. pyhive.py 已从目录中删除

尝试过的可能解决方案: 1.There 安装了 python2.7 和 python 3.4 两个版本。我 已卸载 Python3.4 但该文件夹似乎仍存在于 /usr/local/lib/.我 运行 下面的一些命令来检查我的 python 的安装位置和 PYTHONPATH

中可用的包
vaibhav@vaibhav-Lenovo-G570:~$ which -a python
/usr/bin/python
vaibhav@vaibhav-Lenovo-G570:~$ python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 '/home/vaibhav',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/vaibhav/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

2。从 link 提到的 here 中获得参考,他们提到在虚拟环境中使用它或使用干净的 anaconda。没有使用它们中的任何一个,也不知道它将如何影响现有的配置。

3.I 使用 sudo 安装 Pyhive,所以我更改了权限 link 但仍然遇到同样的问题。

您正在启动的文件名为 pyhive.py

当你这样做时

from pyhive import hive

在您的 pyhive.py 中,然后它将尝试从 您的 模块而不是 pyhive 库中导入 hive

请将您正在启动的文件命名为其他名称,并避免使用现有的名称 modules/libraries。

来自docs

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.