Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 509
Sebastian Olsen Avatar asked Apr 17 '26 08:04

Sebastian Olsen


1 Answers

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
like image 71
Derek Kwok Avatar answered Apr 20 '26 11:04

Derek Kwok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!