djangopython-memcached

django cache.set() Type error


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


Solution

  • 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