Python:LookupError:未知编码:十六进制

时间:2011-10-17 14:10:36

标签: python api encoding twitter

我正在使用名为Tweepy的Twitter API。它一直工作正常,然后我重新启动服务器,现在我得到了这个:

>>> import tweepy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tweepy/__init__.py", line 17, in <module>
    from tweepy.streaming import Stream, StreamListener
  File "tweepy/streaming.py", line 16, in <module>
    json = import_simplejson()
  File "tweepy/utils.py", line 83, in import_simplejson
    import simplejson as json
  File "build/bdist.linux-i686/egg/simplejson/__init__.py", line 111, in <module>

  File "build/bdist.linux-i686/egg/simplejson/decoder.py", line 29, in <module>
  File "build/bdist.linux-i686/egg/simplejson/decoder.py", line 21, in _floatconstants
LookupError: unknown encoding: hex

我也试过了:

from encodings import hex_codec

并没有帮助。

任何想法为什么?

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题(使用json),解决方案是在'encodings'Lib文件夹中复制丢失的'hex_codec.py'文件:

确保安装了Python 2.7.X(以后会发现它很有用)。

  1. 转到“编码”文件夹的Python 2.7.X安装目录(例如,对于Win平台:'C:\ Python27 \ Lib \ encodings')
  2. 将'hex_codec.py'或每个'.py'文件复制到'build / bdist.linux-i686 / egg / encodings'文件夹中。 ...
  3. PROFIT

答案 1 :(得分:1)

我也遇到过这个问题,但是在我无法访问的计算机上发生了这个问题。

我重命名了编码/ hex_codec.py(并删除了.pyc) 看起来会发生这种错误

ActivePython 2.7.0.2 (ActiveState Software Inc.) based on
Python 2.7 (r27:82500, Aug 23 2010, 17:18:21) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\json\__init__.py", line 108, in <module>
    from .decoder import JSONDecoder
  File "c:\Python27\lib\json\decoder.py", line 24, in <module>
    NaN, PosInf, NegInf = _floatconstants()
  File "c:\Python27\lib\json\decoder.py", line 18, in _floatconstants
    _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
LookupError: unknown encoding: hex
>>>

检查你的文件,可能缺少hex_encoding。 或者你的PATH / PYTHON_HOME中可能有一些奇怪的东西

你可以在运行代码之前做printenv吗?和解释器中的“print sys.path”?

答案 2 :(得分:1)

我在尝试android-python27(https://code.google.com/p/android-python27/issues/detail?id=30)时遇到了同样的问题。

通过从PyPy复制binascii.py版本解决,并修改为尝试binascii.so并回退到pure-python如果无法导入二进制文件。

在此处获取结果:https://gist.github.com/alanjds/6091309

答案 3 :(得分:0)

您是否更改了Python安装?在3.x中,似乎比2.x中的编解码器少。特别是,缺少hexbase64,AFAIK。