我正在为电子商务网站编写注册/登录系统,并使用flask-security(http://pythonhosted.org/Flask-Security/)来处理注册功能。部分基本设置需要以下signup.py模块:
from flask.ext.security import SQLAlchemyUserDatastore, Security
from app.models import User, Role
from app import app, db
# Setup Flask Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
然后我必须将user_datastore和安全对象导入到views.py模块中,如下所示:
from app.signup import user_datastore, security
问题是,只要我将上面的import语句包含到我的视图模块中,我的整个应用程序崩溃了,当我尝试运行我的单元或行为测试时,我得到以下回溯错误(为了便于阅读而编辑)
======================================================================
ERROR: Failure: AttributeError ('_FakeSignal' object has no attribute 'connect_via')
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
addr.filename, addr.module)
File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/tests/test_database.py", line 6, in <module>
from app import app, db, models
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/__init__.py", line 9, in <module>
from app import views, models
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/views.py", line 7, in <module>
from app.signup import user_datastore
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/signup.py", line 7, in <module>
security = Security(app, user_datastore)
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/flask/lib/python2.7/site-packages/flask_security/core.py", line 346, in __init__
self._state = self.init_app(app, datastore, **kwargs)
File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/flask/lib/python2.7/site-packages/flask_security/core.py", line 368, in init_app
identity_loaded.connect_via(app)(_on_identity_loaded)
AttributeError: '_FakeSignal' object has no attribute 'connect_via'
我不知道这意味着什么。我试过阅读烧瓶安全文档,但我不明白为什么会这样。感谢任何对此进行抨击的人!
答案 0 :(得分:15)
简答:您缺少blinker库。 编辑:您确认您的虚拟环境无法找到闪烁并重新安装。
长答案:
我认为错误来自Flask Signals。从信号中查看此代码:
signals_available = False
try:
from blinker import Namespace
signals_available = True
except ImportError:
class Namespace(object):
def signal(self, name, doc=None):
return _FakeSignal(name, doc)
所以我认为代码试图找到blinker
库,在你的情况下,它无法导入它,因此它尝试使用_FakeSignal
类。
_FakeSignal
类未定义connect_via
属性,如下所示
class _FakeSignal(object):
"""If blinker is unavailable, create a fake class with the same
interface that allows sending of signals but will fail with an
error on anything else. Instead of doing anything on send, it
will just ignore the arguments and do nothing instead.
"""
def __init__(self, name, doc=None):
self.name = name
self.__doc__ = doc
def _fail(self, *args, **kwargs):
raise RuntimeError('signalling support is unavailable '
'because the blinker library is '
'not installed.')
send = lambda *a, **kw: None
connect = disconnect = has_receivers_for = receivers_for = \
temporarily_connected_to = connected_to = _fail
del _fail
Flask-Security尝试加载的connect_via
属性实际上是由blinker
库提供的,因为没有闪烁,没有connect_via。因此它失败了。
所以你应该先安装闪光灯。但是,我认为在尝试使用blinker
之前,Flask-Security代码也应检查connect_via
。
答案 1 :(得分:0)
致未来的互联网人:
我有相同的错误,但原因不同。我正在使用anaconda的虚拟环境,并且认为它与flask的自动重新加载系统冲突(它一次无法正确加载,并且修复了错误也没有改变任何东西,即使编辑了blinker.py文件也是如此)。打开一个新控制台以修复该问题。
抱歉,我没有更多信息可提供,但希望对您有所帮助。