1

我正在阅读使用 Vagrant 和 Ansbile ( https://github.com/nickhs/flask_skeleton ) 部署 Flask 应用程序的指南,但一直遇到我不知道如何修复的错误。

我运行以下... $ vagrant up db

...在我找不到任何答案的错误之前经历了相当多的步骤。

这些是完成的步骤...

[db] 运行配置器:ansible...

PLAY [dbservers] ************************************************************** 

GATHERING FACTS *************************************************************** 

TASK: [update apt] ************************************************************ 
changed: [192.168.100.20]

TASK: [install base packages] ************************************************* 
changed: [192.168.100.20] => (item=libpq-dev,python-dev,python-setuptools,git,libevent-dev,python-psycopg2)

TASK: [install pip] *********************************************************** 
changed: [192.168.100.20]

TASK: [add env variables] ***************************************************** 
changed: [192.168.100.20]

TASK: [install db server packages] ******************************************** 
changed: [192.168.100.20] => (item=postgresql,postgresql-contrib)

TASK: [update postgres db configuration] ************************************** 
changed: [192.168.100.20]

TASK: [update postgres client configuration] ********************************** 
changed: [192.168.100.20]

TASK: [link /vagrant to /srv/flask_skeleton] ********************************** 
changed: [192.168.100.20]

TASK: [install dependencies] ************************************************** 
changed: [192.168.100.20]

NOTIFIED: [source env variables] ********************************************** 
ok: [192.168.100.20]

NOTIFIED: [restart postgresql] ************************************************ 

PLAY [dbservers] ************************************************************** 
TASK: [create database user] ************************************************** 
TASK: [create database] ******************************************************* 

NOTIFIED: [build database] **************************************************** 
failed: [192.168.100.20] => {"item": "", "rc": 1}
stdout: Traceback (most recent call last):
  File "/srv/flask_skeleton/createdb.py", line 1, in <module>
    from app import db
ImportError: cannot import name db


PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/var/tmp/ansible/vagrant-dbserver.retry

192.168.100.20             : ok=14   changed=12   unreachable=0    failed=1   

谁能指出我正确的方向?

4

1 回答 1

1

查看 app.py 中的行:

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')

if app.config['SQLALCHEMY_DATABASE_URI']:
    db = SQLAlchemy(app)

如果您没有定义 DATABASE_URL,那么将不会创建 'db' 变量,然后无法在 createdb.py 模块中导入该变量。你能检查一下你是否确实有 os.environ.get('DATABASE_URL') 的值吗?

于 2013-07-24T19:06:47.473 回答