" RuntimeError:在应用程序上下文之外工作"用于配置模块

时间:2016-08-13 04:22:46

标签: python flask jinja2

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

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行没有代码,如错误提示

1 个答案:

答案 0 :(得分:0)

我最终使用的是卡林所描述的。

index.py

from __future__ import print_function # For printing output to CMD
import sys

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

app = Flask(__name__)

from config import * 
// ... rest of code for main file (like routes and stuff) below

config.py

from flask import Flask, current_app

app = Flask(__name__)

with app.app_context():
    // code goes here ...
    // in this case, I'm configuring various services like API's and jinja2