Fixing ImportError: cannot import name ‘urlencode’ in Python3

So 08 Februar 2015
By Uli Köhler

In Python

Problem:

Your python 3.x interpreter prints the error message

ImportError: cannot import name 'urlencode'

Solution:

As described here on SO, Python3 contains urlencode() not in the urllib module but in urllib.parse.

Therefore you have to change

from urllib import urlencode

to

from urllib.parse import urlencode

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

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