我正在编写使用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
有人能指出我正确的方向吗?
答案 0 :(得分: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')的值?