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

时间:2017-06-25 09:04:53

标签: python python-2.7 hadoop hive pyhive

我想使用pyhive将Python连接到hive。我正在使用下面的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已从目录中删除
  2. 尝试了可能的解决方案: 1.安装了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。从他们提到的here提到的链接中获得参考,以便在虚拟环境中使用它或使用干净的anaconda。没有使用它们如何以及不知道它将如何影响已有的配置。

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

1 个答案:

答案 0 :(得分:0)

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

当你这样做时

from pyhive import hive

pyhive.py内,然后会尝试从您的模块导入hive,而不是从pyhive库导入。{/ p>

请为您启动其他文件命名,并避免使用现有模块/库的名称。

来自docs

  

当导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。如果未找到,则会在变量sys.path给出的目录列表中搜索名为spam.py的文件。 sys.path从这些位置初始化:

     
      
  • 包含输入脚本(或当前目录)的目录。
  •   
  • PYTHONPATH(目录名列表,语法与shell变量PATH相同)。
  •   
  • 依赖于安装的默认值。
  •