> For the complete documentation index, see [llms.txt](https://flask-yushu.gitbook.io/yushu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flask-yushu.gitbook.io/yushu/4.flask-he-xin-ji-zhi/4.1-flask-zhong-jing-dian-cuo-wu-working-outside-application-context.md).

# 4.1 flask中经典错误 working outside application context

在 3.8节我们通过`db.create_all(app=app)`的方式解决了working outside application context的错误，下面我们来深究，这个错误出现的具体原因是什么。

首先写一段测试代码

```python
from flask import Flask, current_app


app = Flask(__name__)

# 断点调试这里显示current_app=[LocalProxy]<LocalProxy unbound>
a = current_app

# RuntimeError: Working outside of application context.
b = current_app.config["DEBUG"]
```

我们通过current\_app获取配置，看似没有问题的代码，却抛出了同样的异常。

通过断点调试发现current\_app并不是Flask对象，而是一个unbound的LocalProxy。

回想我们之前的request对象，其实也是个LocalProxy。

```python
# context locals
_request_ctx_stack = LocalStack()
_app_ctx_stack = LocalStack()
current_app = LocalProxy(_find_app)
request = LocalProxy(partial(_lookup_req_object, 'request'))
session = LocalProxy(partial(_lookup_req_object, 'session'))
g = LocalProxy(partial(_lookup_app_object, 'g'))
```

那么这里为什么会抛出这个异常呢，想要回答这个问题，就需要深入理解这个LocalProxy。我们在下一小节进行介绍
