问题
I am using Flask-Session and recently installed or upgraded to itsdangerous 1.0.0. Now I get the error ImportError: cannot import name 'want_bytes'
. Why is this error happening and how can I fix it?
from flask import Flask, session
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
Traceback (most recent call last):
File "c:\program files\python36\lib\site-packages\flask\cli.py", line 330, in __call__
rv = self._load_unlocked()
File "c:\program files\python36\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "c:\program files\python36\lib\site-packages\flask\cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "c:\program files\python36\lib\site-packages\flask\cli.py", line 242, in locate_app
'\n\n{tb}'.format(name=module_name, tb=traceback.format_exc())
flask.cli.NoAppException: While importing "application", an ImportError was raised:
Traceback (most recent call last):
File "c:\program files\python36\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "C:\Program Files\Python36\learningPython\web_CS50\project1\application.py", line 4, in <module>
from flask_session import Session
File "c:\program files\python36\lib\site-packages\flask_session\__init__.py", line 16, in <module>
from .sessions import NullSessionInterface, RedisSessionInterface, \
File "c:\program files\python36\lib\site-packages\flask_session\sessions.py", line 23, in <module>
from itsdangerous import Signer, BadSignature, want_bytes
ImportError: cannot import name 'want_bytes'
I am stumped on this one and have not been able to find an applicable or useful answer here on stackoverflow, or anywhere for that matter.
回答1:
itsdangerous 1.1.0 contains a temporary fix for this. You can upgrade itsdangerous and continue to use Flask-Session right now. There is no need to pin to itsdangerous==0.24.
itsdangerous 1.0.0 was recently released, removing non-public top-level imports. Flask-Session appears to have been using such an import, from itsdangerous import want_bytes
.
This has been reported to Flask-Session and a fix will hopefully be released eventually.
回答2:
The file structure of the itsdangerous package has changed. The function resides in the session.py file. Changing the import statements from:
from itsdangerous import Signer, BadSignature, want_bytes
to
from itsdangerous import Signer, BadSignature
from itsdangerous.encoding import want_bytes
works for me.
来源:https://stackoverflow.com/questions/52900312/flask-session-cant-import-want-bytes-from-itsdangerous