Колба, как получить элемент конфигурации из другого файла (например: helper.py) - PullRequest
0 голосов
/ 10 декабря 2018

Добрый день.У меня есть один вопрос.Как получить доступ к элементу конфигурации из другого файла .py

Например.

app / __ init __. Py

def create_app(config=None):
    app = Flask(__name__, instance_relative_config=True)   
    app._static_folder = './static'
    app.config.from_pyfile('flask.cfg')
    app.app_context().push()
    return app

wsgi.py

from app import create_app
from werkzeug.debug import DebuggedApplication


if __name__ == "__main__":
    app = create_app()
    application = DebuggedApplication(app, evalex=True)    
    app.logger.info("Debug status is: " + str(app.config['DEBUG']))
    app.run(use_reloader=True, debug=True)

app / core / helper.py

from flask import current_app as app

def get_dir():
    return app.config["PATH_CACHE_DIR"]
  

Попробуйте вызвать метод app / core / cached.py

from flask_caching import Cache
from flask import current_app as app
from app.core.helper import get_dir

print get_dir()

Итак, я получаю следующую ошибку

  Traceback (most recent call last):
  File "/home/stanislav/Python/mbgp/wsgi.py", line 1, in <module>
    from app import create_app
  File "/home/stanislav/Python/mbgp/app/__init__.py", line 4, in <module>
    from app.core.cached import cache
  File "/home/stanislav/Python/mbgp/app/core/cached.py", line 5, in <module>
    print get_dir()
  File "/home/stanislav/Python/mbgp/app/core/helper.py", line 14, in get_dir
    return app.config["PATH_CACHE_DIR"]
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 51, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current applicat`enter code here`ion object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

Подскажите пожалуйста, как это исправить, большое спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...