python - "local variable ' e ' referenced before assignment"这个错误是什么意思?我该如何解决这个错误?

标签 python python-3.x artificial-intelligence

我正在尝试使用 del.icio.us API 并遵循 Programming Collective Intelligence

一书中的示例

当我在 python 3.6.2 中使用这些命令时:

>>> 从 deliciorec 导入 *

>>> delusers=initializeUserDict('编程')

我收到这个错误:

<urlopen error [Errno 11001] getaddrinfo failed>, 4 tries left.  
<urlopen error [Errno 11001] getaddrinfo failed>, 3 tries left.  
<urlopen error [Errno 11001] getaddrinfo failed>, 2 tries left. 
<urlopen error [Errno 11001] getaddrinfo failed>, 1 tries left.  
Traceback (most recent call last):               
   File "<stdin>", line 1, in <module> 
   File"C:\Users\user\AppData\Local\Programs\Python\Python36\deliciousrec.py", line 10, in initializeUserDict            
     for p1 in get_popular(tag=tag)[0:count]:   
   File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pydelicious-0.6-py3.6.egg\pydelicious\__init__.py", line 1042, in get_popular        
   File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pydelicious-0.6-py3.6.egg\pydelicious\__init__.py", line 1026, in getrss    
   File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pydelicious-0.6-py3.6.egg\pydelicious\__init__.py", line 455, in dlcs_rss_request      
   File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pydelicious-0.6-py3.6.egg\pydelicious\__init__.py", line 239, in http_request                    
   UnboundLocalError: local variable 'e' referenced before assignment   

我无法打开 pydelicious-0.6-py3.6.egg 并访问此处要求修改的 init 文件。 以前有没有人见过这种类型的错误。我该如何解决?

最佳答案

本图书馆的 PyPI page是用过去时写的:

Was a complete Python interface to del.icio.us Bookmarks’ HTTP API’s.

它的主页是一个 Github 存储库,上面写着:

Old pydelicious work (--2010)

自 2010 年以来的唯一更新是:

[2016] del.icio.us was gone since about 2009, and redirected to delicious.com. At the moment there is another login page back at del.icio.us. Not sure what they're up to now."

甚至the old 2010 page说:

IMPORTANT: pydelicious has not been updated to use the OAuth protocol. New users with a Yahoo account/email will not be able to use this library.

因此,这个库似乎很可能从未针对 Python 3.x 更新过,或者它所依赖的任何库的最新版本,或者任何新的 del.icio.us 是。


但与此同时,如果您想自己调试,也可以。

要查看 .egg 存档中的源代码,您可以在 Python 中执行此操作:

>>> import deliciousrec
>>> import inspect
>>> inspect.getsource(deliciousrec)

... 或者您可以使用您最喜欢的 ZIP 文件工具提取 .egg 存档,因为所有 .egg 文件都是这样。

... 或者您可以直接克隆 GitHub 存储库,或 view it online ,这可能是最好的解决方案。


正如 Hamms 在评论中指出的那样,如果您查看 the function that's raising ,它正在这样做:

except urllib2.HTTPError, e:
    # reraise unexpected protocol errors as PyDeliciousException
    raise PyDeliciousException, "%s" % e

在 Python 2 中,这意味着处理 urllib2.HTTPError 类型的异常,并将 e 绑定(bind)到函数其余部分的异常。

在 Python 2.6 中,这已被软弃用,取而代之的是这种新语法:

except urllib2.HTTPError as e:
    # reraise unexpected protocol errors as PyDeliciousException
    raise PyDeliciousException, "%s" % e

在 Python 3 中,旧语法完全消失,因此现在是非法的。

2to3 工具会自动为您解决这个特定问题,但它可能无法解决所有问题;您需要手动将代码移植到 3.x。当然,这可能没有任何好处,因为您正在尝试调用 2010 年消失的 API。

关于python - "local variable ' e ' referenced before assignment"这个错误是什么意思?我该如何解决这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51713606/

相关文章:

xml - 如何让 <topic> 标签在 AIML 中处理这个问题?

java - ffnet java 导出示例

python - 使用 python-django 访问 sqlite 中的日期时间字段

python - 尝试计算 .txt 文件中的特定字符串 (python)

machine-learning - 机器学习 - 亥姆霍兹机器实现

python - 如何在 Python 3 中相应地将括号放入列表中?

python - 在 Python 3.4 中使用 Kivy

python - 撞车时如何清理?

python - 从模块导入多个类的语法

python-3.x - 在电池jupyter笔记本中关闭警告