How to Fix AttributeError: ‘module’ object has no attribute ‘urlencode’

‘module’ object has no attribute ‘urlencode’

AttributeError: ‘module’ object has no attribute ‘urlencode’ error occurs because, in Python 3, “urlencode has been moved to the urllib.parse module.”

How to fix it?

To fix the AttributeError: ‘module’ object has no attribute ‘urlencode’ error, you should import urlencode from urllib.parse module.

For Python 3

The urlencode has been moved to the urllib.parse module.

from urllib.parse import urlencode

query_string = urlencode({"key": "value"})

For Python 2

The urlencode is available in the urllib module.

import urllib

query_string = urllib.urlencode({"key": "value"})

Conclusion

If you are using Python 3 (which is likely, given that Python 2 is no longer officially supported), you should import urlencode from urllib.parse.

Related posts

AttributeError: ‘module’ object has no attribute ‘urlopen’

AttributeError: ‘module’ object has no attribute ‘urlretrieve’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.