python - "RuntimeError: working outside of application context"用于配置模块

标签 python flask jinja2

我有一个从主文件调用的 config.py 模块,它有用于 jinja2filters 的各种 app.config 以及我正在使用的其他一些插件:

config.py 摘录:

from flask import current_app as app  
#imports go here

def function1:
    print("hello")

app.config['PROPERTY_1'] = 'configgoeshere'

#jinja2 functions and configs would go here like
app.jinja_env.filters['datetime'] = datetimeformat

从index.py调用:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory

app = Flask(__name__)
app.config['DEBUG'] = False

app.config.from_object('config') #config file

############################################################################################################################
# MAIN ROUTES
############################################################################################################################
# The route for the homepage
@app.route('/')
def index():

返回错误:

RuntimeError: working outside of application context

更新 我现在正在尝试传递应用程序上下文,这里是index.py:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory
import time, datetime, os, urllib, urllib2, urlparse, requests, json, string
from urllib2 import Request, urlopen, URLError, HTTPError, urlparse

app = Flask(__name__)
app.config['DEBUG'] = False

app.config.from_object('config')


############################################################################################################################
# MAIN ROUTES
############################################################################################################################
# The route for the homepage
@app.route('/')
def index():

这是更新后的 config.py 摘录:

from index import app
#imports go here

def function1:
    print("hello")

app.config['PROPERTY_1'] = 'configgoeshere'

#jinja2 functions and configs would go here like
app.jinja_env.filters['datetime'] = datetimeformat

返回的错误是:

$ python index.py
Traceback (most recent call last):
  File "index.py", line 8, in <module>
    app.config.from_object('config')
  File "C:\Python27\lib\site-packages\flask\config.py", line 162, in from_object
    obj = import_string(obj)
  File "C:\Python27\lib\site-packages\werkzeug\utils.py", line 418, in import_string
    __import__(import_name)
  File "C:\Users\****\Desktop\*****\config.py", line 1, in <module>
    from index import app
  File "C:\Users\***\Desktop\*****\index.py", line 17, in <module>
    @app.route('/')
  File "C:\Python27\lib\site-packages\werkzeug\local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Python27\lib\site-packages\werkzeug\local.py", line 302, in _get_current_object
    return self.__local()
  File "C:\Python27\lib\site-packages\flask\globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

注意 - config.py 中的第 162 行没有像错误所示的代码

最佳答案

查看 Flask 对 Application Contexts 的解释.

在您的 config.py 文件中,fromflask import current_app as app 使得对 app.config['PROPERTY_1'] = 的调用'configgoeshere' 实际上尝试在 current_app 上设置配置,尽管在请求到来之前默认没有应用程序上下文(因此出现错误)。由于该调用不在函数内,因此它会在其他任何事情(例如请求)发生之前立即执行。

我建议在索引中的 app 实例上进行配置,而不是在 current_app 上。

关于python - "RuntimeError: working outside of application context"用于配置模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38929153/

相关文章:

python - 是否有更有效的方法将数据框(列和数据)写入列表?

Python Selenium : select option

python - 如何将 flask 配置为可通过公共(public) IP 接口(interface)访问?

javascript - Python Flask 模板使用 {{}} 不能混合 javascript var

ansible - 在 Ansible 中循环嵌套字典

dictionary - 使用 Ansible 将嵌套字典扁平化为键/值对

python - 使用 SVM 的 coef_ 函数的特征重要性

python - 如何使 argparse 参数中的多个选择成为默认值,但一个或多个选择是可选的?

javascript - Dropzone 上传到 Flask 应用后不会重定向

ssl - 如何使用 SSL 将 AWS EC2 Flask 应用程序部署到 HTTPS 端口 443?