2016-10-11 38 views
4

我在Heroku上使用bcrypt和我的Flask應用程序時出現問題。當我部署到Heroku並轉到登錄路由時,我得到500內部服務器錯誤。它在本地正常工作。我如何獲得在Heroku上運行的bcrypt包?Heroku上的Python bcrypt包給AttributeError:'module'對象沒有屬性'ffi'

ERROR in app: Exception on /login [POST] 
Traceback (most recent call last): 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper 
    resp = resource(*args, **kwargs) 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask/views.py", line 84, in view 
    return self.dispatch_request(*args, **kwargs) 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 587, in dispatch_request 
    resp = meth(*args, **kwargs) 
    File "/app/app.py", line 196, in post 
    elif bcrypt.check_password_hash(user.password, password): 
    File "/app/.heroku/python/lib/python2.7/site-packages/flask_bcrypt.py", line 193, in check_password_hash 
    return safe_str_cmp(bcrypt.hashpw(password, pw_hash), pw_hash) 
    File "/app/.heroku/python/lib/python2.7/site-packages/bcrypt/__init__.py", line 82, in hashpw 
    hashed = _bcrypt.ffi.new("char[]", 128) 
AttributeError: 'module' object has no attribute 'ffi' 

回答

0

我已經找到了解決辦法,我用的是下面的包:bcryptflask_bcryptpy-crypt。所以我卸載了py-bcrypt,可能這個包與bcrypt包有衝突。

pip uninstall py-bcrypt 
+1

是否知道可能導致此問題的任何其他包?我沒有安裝py-bcrypt,但也面臨着同樣的錯誤信息。謝謝 – user3939059

4

我遇到過類似的問題。這裏是我的堆棧跟蹤的最後一部分的副本:

self.password = User.hashed_password(password) 
File "/app/application/models.py", line 16, in hashed_password 
File "/app/.heroku/python/lib/python3.5/site-packages/flask_bcrypt.py", line 163, in generate_password_hash 
File "/app/.heroku/python/lib/python3.5/site-packages/bcrypt/__init__.py", line 50, in gensalt 
    output = _bcrypt.ffi.new("unsigned char[]", 30) 
AttributeError: module 'bcrypt._bcrypt' has no attribute 'ffi' 

我想知道這個問題是否特定於Heroku。我正在使用一些現有的Flask樣板。但是在使用Heroku上的(不同的)boilerplate Flask項目時,以前的項目中也遇到了Bcrypt的這個問題。

可能的解決方案1個

不同的依賴性組合玩耍。在一個案例中,當我在我的requirements.txt中包含cryptography時,問題就消失了。但是正如讓席爾瓦在這篇文章中提到的那樣,依賴關係可能會發生衝突。所以你可能想玩不同的組合,直到有效。

可能的解決方案2

如果使用的燒瓶中,嘗試具有werkzeug.security包/模塊來散列/檢查散列,而不是直接使用bcrypt包。在下面的示例中,在我的models.py中,註釋掉這些行並添加新行解決了我的問題。

# from index import db, bcrypt 
from index import db 
from werkzeug.security import generate_password_hash, check_password_hash 


class User(db.Model): 
    id = db.Column(db.Integer(), primary_key=True) 
    email = db.Column(db.String(255), unique=True) 
    password = db.Column(db.String(255)) 

    def __init__(self, email, password): 
     self.email = email 
     self.active = True 
     self.password = User.hashed_password(password) 

    @staticmethod 
    def hashed_password(password): 
     # return bcrypt.generate_password_hash(password) 
     return generate_password_hash(password) 

    @staticmethod 
    def get_user_with_email_and_password(email, password): 
     user = User.query.filter_by(email=email).first() 
     # if user and bcrypt.check_password_hash(user.password, password): 
     if user and check_password_hash(user.password, password): 
      return user 
     else: 
      return None 
1

通過安裝bcrypt == 3.1.2這是爲我工作

pip install bcrypt==3.1.2 
0

卸載都PY-bcrypt和bcrypt是你以前安裝它。 然後重新安裝py-bcrypt。

pip install py-bcrypt

相關問題