Fixing ImportError: cannot import name 'urlencode' in Python3

Problem:

Your python 3.x interpreter prints the error message

importerror.txt
ImportError: cannot import name 'urlencode'

Solution

As described by Fred Foo here on StackOverflow, Python3 contains urlencode() not in the urllib module but in urllib.parse.

Therefore you have to change

fix_urlencode.py
from urllib import urlencode

to

fix_compat_urlencode.py
from urllib.parse import urlencode

If you intend to write code for both Python2 and Python3, prefer using:

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

Check out similar posts by category: Python