Python Cherrypy: 500 ValueError: Page handlers MUST return bytes

I my case the issue started after switching from python2 to python3.

It was resolved by setting

    'tools.encode.text_only': False

In the app global configuration.

Hope it helps


You need to rearrange global config update to happen after application mount:

config = {
}

cherrypy.tree.mount(Root(), '/', config=config)

cherrypy.config.update({
    'tools.staticdir.debug': True,
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': *****,
    'tools.sessions.on': True,
    'tools.encode.on': True,
    'tools.encode.encoding': 'utf-8'
})

cherrypy.engine.start()

Because you were calling config = {} after your config update command you were overriding the update settings for Root application.

Also, change your submit function to this:

@cherrypy.expose
@cherrypy.tools.json_out
def submit(self, myfile):
    cherrypy.session['myfile'] = myfile

    # Return dict, which will be autoconverted to JSON
    # by the json_out tool (see decorator above)
    return {'title': myfile.filename}

Hi people looking for answers. I had the same problem but in my case this little addition solved everything.

return <some-json>.encode('utf8')