修复 Python3 中 ImportError: cannot import name 'urlencode'

问题:

你的 python 3.x 解释器打印错误消息

importerror.txt
ImportError: cannot import name 'urlencode'

解决方案

正如 Fred Foo 在 StackOverflow 上所述,Python3 中的 urlencode() 不在 urllib 模块中而在 urllib.parse 中。

因此你必须更改

fix_urlencode.py
from urllib import urlencode

to

fix_compat_urlencode.py
from urllib.parse import urlencode

如果你打算为 Python2 和 Python3 编写代码,优先使用:

fix_compat_tryexcept.py
try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode

Check out similar posts by category: Python