从 Flask Web开发 17.4.2学习并使用Heroku本地测试,执行heroku local: run flask
部署时发生以下错误:
Traceback (most recent call last):
File "C:\Users\KodX\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\KodX\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "F:\flasky\venv\Scripts\flask.exe\__main__.py", line 9, in <module>
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "f:\flasky\venv\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "f:\flasky\venv\lib\site-packages\click\core.py", line 1132, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args)
File "f:\flasky\venv\lib\site-packages\click\core.py", line 1171, in resolve_command
cmd = self.get_command(ctx, cmd_name)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 515, in get_command
rv = info.load_app().cli.get_command(ctx, name)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 377, in load_app
raise_if_not_found=False)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 254, in locate_app
return find_best_app(script_info, module)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 76, in find_best_app
app = call_factory(script_info, app_factory)
File "f:\flasky\venv\lib\site-packages\flask\cli.py", line 114, in call_factory
return app_factory(script_info)
File "F:\flasky\app\__init__.py", line 22, in create_app
app.config.from_object(config[config_name])
KeyError: <flask.cli.ScriptInfo object at 0x00000191FA23ABA8>
__init__.py
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_pagedown import PageDown
from config import config
bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
pagedown = PageDown()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
db.init_app(app)
login_manager.init_app(app)
pagedown.init_app(app)
if app.config['SSL_REDIRECT']:
from flask_sslify import SSLify
sslify = SSLify(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')
from .api import api as api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api/v1')
return app
环境版本详细信息
Operating System: Windows 10
Python:3.7
Flask: 1.0.2
参考书
Flask Web开发版本2 17.4.2使用Heroku本地测试
Heroku CLI具有一个本地命令,该命令在非常接近Heroku服务器的环境中本地测试应用程序。但是,在本地运行应用程序时,诸如FLASK_APP之类的环境变量不再是环境变量。 heroku local命令在中查找配置应用程序的环境变量。应用程序顶部目录中的env文件。例如。 env文件可能包含以下变量:
FLASK_APP=flasky.py
FLASK_CONFIG=heroku
MAIL_USERNAME=
MAIL_PASSWORD=
自。 env文件包含密码和其他敏感帐户信息,请勿将其包含在版本控制中。
在启动应用程序之前执行部署任务和数据库创建。可以使用local:run命令运行一次任务:
(venv) $heroku local: run flask deploy
[OKAY] Loaded ENV.env File as KEY = VALUE Format
INFO Context impl SQLiteImpl.
INFO Will assume non-transactional DDL.
INFO Running upgrade - > 38c4e85512a9, initial migration
INFO Running upgrade 38c4e85512a9 - > 456a945560f6, login support
INFO Running upgrade 456a945560f6 - > 190163627111, account confirmation
INFO Running upgrade 190163627111 - > 56ed7d33de8d, user roles
INFO Running upgrade 56ed7d33de8d - > d66f086b258, user information
INFO Running upgrade d66f086b258 - > 198b0eebcf9, caching of avatar hashes
INFO Running upgrade 198b0eebcf9 - > 1b966e7f4b9e, post model
INFO Running upgrade 1b966e7f4b9e - > 288cd3dc5a8, rich text posts
INFO Running upgrade 288cd3dc5a8 - > 2356a38169ea, followers
INFO Running upgrade 2356a38169ea - > 51f5ccfba190, comments