3

I'm using django 1.9 and Python 3. I've installed python-memcached and when I try to cache.set("foo", "bar") I get this error:

TypeError: a bytes-like object is required, not 'str'

Any idea why this is happening? I made sure to set the middleware and add the cache to settings.py

Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91

1 Answers1

4

The error comes from string/unicode changes in Python 3. In Python 2, string type are bytes, but in Python 3 string type is separate from byte type - which is causing the error you're seeing.

If you're using Python 3.x with memcached, use the python3-memcached package. It's a drop in replacement for python-memcached.

pip install python3-memcached
Derek Kwok
  • 12,768
  • 6
  • 38
  • 58