在开发过程中如何重命名Flask WSGI App?

时间:2019-10-09 07:33:39

标签: python flask environment-variables virtualenv pipenv

我想知道在开发阶段如何更改Flask WSGI App的名称。

使用Flask Mega-Tutorial作为参考,我能够成功设置“ Hello World”应用程序。
教程中的离题

  1. 使用pipenv作为我的Python虚拟环境管理器(而不是venv
  2. 应用程序的名称为astronomer.py

现在,我想在现有应用程序的基础上构建并根据我的要求自定义代码;以我在.flaskenv文件中定义为FLASK_APP env var的应用程序名称开头。 因此,我已将根级别Python脚本的名称从astronomer.py(在本教程中)更新为galielo.py(供我使用)。在更改了FLASK_APP的相应值并通过$ pipenv run flask run重新启动Flask服务器之后,该应用程序崩溃并出现以下错误:

$ pipenv run flask run                                                                                                                                                                                                             [12:29:33]
 * Serving Flask app "astronomer.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 302-012-958
127.0.0.1 - - [09/Oct/2019 12:29:57] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
ModuleNotFoundError: No module named 'astronomer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 338, in __call__
    self._flush_bg_loading_exception()
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 326, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 314, in _load_app
    self._load_unlocked()
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/Users/kshitij10496/.local/share/virtualenvs/galileo-iQPdbs28/lib/python3.7/site-packages/flask/cli.py", line 250, in locate_app
    raise NoAppException('Could not import "{name}".'.format(name=module_name))
flask.cli.NoAppException: Could not import "astronomer".

调试

  • 登录到虚拟环境并检查环境变量FLASK_APP的值后,我得到了旧值astronomer.py。这解释了为什么应用程序无法启动。但是,我不明白为什么会这样?
  • 我什至尝试使用$ pipenv run flask run --eager-loading使用“紧急加载”应用程序 尽管如此,该应用程序仍不会以相同的课程错误信息开始。

我能够通过从虚拟环境中取消设置环境变量FLASK_APP并重新启动Flask服务器来手动解决此问题。我很想知道为什么应用程序在初始化时没有加载文件.flaskenv,以及是否有自动的方法来实现这一点?

1 个答案:

答案 0 :(得分:0)

对于Pipenv,我认为在环境变量方面有些不同。与per the documentation一样,有一种内置机制可用于加载.env文件:

  

如果您的项目中存在.env文件,则$ pipenv shell和$ pipenv run会自动为您加载

所以我想您应该将文件从.flaskenv重命名为.env,然后安全地删除python-dotenv依赖性。