为什么我的flask命令在Heroku上不起作用?

时间:2019-03-30 21:24:31

标签: heroku flask

我在Flask中有一个简单的CLI,可用来创建伪数据。它在我的本地计算机上运行良好,但在Heroku上却无法运行。这是一个终端会话:

(venv) $ flask create-dummy-data
(venv) $ git push staging master
Everything up-to-date
(venv) $ heroku run bash --remote staging
Running bash on ⬢ app-name... up, run.8533 (Free)
~ $ flask create-dummy-data
Usage: flask [OPTIONS] COMMAND [ARGS]...
Try "flask --help" for help.

Error: No such command "create-dummy-data".

这是我的app/cli.py文件:

import lorem

from app import db
from app.models import Survey, Question, Option, Answer, Response

def register(app):
    @app.cli.command()
    def create_dummy_data():
        """Create lorem ipsum questions and answers for testing."""
        survey = Survey()
        db.session.add(survey)

        for _ in range(3):
            question = Question(survey=survey, category='likert', question=lorem.sentence())
            db.session.add(question)

        for _ in range (2):
            question = Question(survey=survey, category='word', question=lorem.sentence())
            db.session.add(question)

        db.session.commit()

还有我的run.py文件:

from app import create_app, db, cli
from app.models import Survey, Question, Option, Answer, Response

app = create_app()
cli.register(app)

@app.shell_context_processor
def make_shell_context():
    return {'db': db,
            'Survey': Survey,
            'Question': Question,
            'Option': Option,
            'Answer': Answer,
            'Response': Response
            }

为什么这会在本地工作,但不能在Heroku shell上工作?

1 个答案:

答案 0 :(得分:0)

这是最简单的事情:Heroku在暂存环境的变量中没有设置$FLASK_APP。添加后,命令行工具即可运行。

enter image description here