Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] dynaconf init fails with AttributeError: 'Config' object has no attribute 'current_env' if Flask app is detected #705

Closed
ernstki opened this issue Jan 19, 2022 · 1 comment
Labels
bug HIGH High Priority
Milestone

Comments

@ernstki
Copy link

ernstki commented Jan 19, 2022

Hi, everyone. I ran into this problem trying to get started with Dynaconf by running dynaconf init inside an existing Flask project. I was able to reproduce with just a very minimal Flask app in a clean virtualenv (see below).

Upon closer inspection, I see the documentation recommends using the FlaskDynaconf extension. I'm not sure if the "bug" is in the documentation—that is, that the docs should simply say do not run dynaconf init for a Flask project—or in the the dynaconf CLI itself, but it seems like dynaconf init should just work, even if a Flask project is detected.

Anyway, thanks for your consideration, and thanks for all your hard work on Dynaconf.

Describe the bug

dynaconf init yields the following error message if FLASK_APP is set.

$ dynaconf init -f ini
Flask app detected
⚙️  Configuring your Dynaconf environment
------------------------------------------
Traceback (most recent call last):
  File "/path/to/dynaconf/venv/bin/dynaconf", line 8, in <module>
    sys.exit(main())
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/core.py", line 221, in __call__
    def __call__(A,*B,**C):return A.main(*B,**C)
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/core.py", line 205, in main
    H=E.invoke(F)
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/core.py", line 345, in invoke
    with C:return F(C.command.invoke(C))
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/core.py", line 288, in invoke
    if A.callback is not _A:return ctx.invoke(A.callback,**ctx.params)
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/core.py", line 170, in invoke
    with G:return A(*B,**E)
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/vendor/click/decorators.py", line 21, in A
    def A(*A,**B):return f(get_current_context(),*A,**B)
  File "/path/to/dynaconf/venv/lib/python3.7/site-packages/dynaconf/cli.py", line 298, in init
    env = settings.current_env.lower()
AttributeError: 'Config' object has no attribute 'current_env'

To Reproduce
Steps to reproduce the behavior:

  1. create a Flask appfactory.py like so

    from flask import Flask, Blueprint
    
    blueprint = Blueprint('default', __name__)
    
    @blueprint.route('/')
    def index():
        return 'Default route.'
    
    def create_app():
        app = Flask(__name__)
        app.register_blueprint(blueprint)
        return app
  2. set FLASK_APP=appfactory:create_app in the environment

  3. run dynaconf init; fails with the traceback and error message shown above

  4. unset FLASK_APP and try dynaconf init again; works

Environment (please complete the following information):

  • macOS 10.15.7
  • Python 3.7.12 from MacPorts
  • Flask 2.0.2 and Dynaconf 3.1.7 from PyPI, in a fresh virtualenv

Other remarks
It doesn't seem to matter if the Flask app uses the application factory pattern or not. With an app.py like this

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Default route.'

and FLASK_APP=app.py, I get the same traceback when I run dynaconf init.

@ernstki ernstki added the bug label Jan 19, 2022
@ernstki
Copy link
Author

ernstki commented Jan 19, 2022

#264 is possibly related, if only because of the same 'Config' object has no attribute 'current_env' error.

@rochacbruno rochacbruno modified the milestones: 3.2.0, 3.1.8 Mar 31, 2022
@rochacbruno rochacbruno modified the milestones: 3.1.9, 3.2.0 Jun 2, 2022
@rochacbruno rochacbruno added the HIGH High Priority label Jul 8, 2022
obaranov added a commit to obaranov/dynaconf that referenced this issue Jul 26, 2022
obaranov added a commit to obaranov/dynaconf that referenced this issue Jul 26, 2022
obaranov added a commit to obaranov/dynaconf that referenced this issue Jul 26, 2022
obaranov added a commit to obaranov/dynaconf that referenced this issue Jul 26, 2022
obaranov added a commit to obaranov/dynaconf that referenced this issue Jul 26, 2022
rochacbruno added a commit that referenced this issue Jul 27, 2022
Co-authored-by: Bruno Rocha <rochacbruno@users.noreply.github.com>
rochacbruno added a commit that referenced this issue Sep 22, 2022
Shortlog of commits since last release:

    Amadou Crookes (1):
          envars.md typo fix (#786)

    Bruno Rocha (19):
          Release version 3.1.9
          Bump dev version to 3.1.10
          Update badges
          demo repo will be replaced by a video tutorial soon
          Fix CI
          New data key casing must adapt to existing key casing (#795)
          Add test and docs about includes (#796)
          Removed vendor_src folder (#798)
          Replacing rochacbruno/ with dynaconf/ (#800)
          Fix codecov (#801)
          Parse negative numbers from envvar Fix #799 and Fix #585 (#802)
          Fix get command with Django (#804)
          Add a functional test runner (#805)
          Test runner docs and styling (#806)
          Allow merge_unique on lists when merge_enabled=True (#810)
          Rebind current env when forced for Pytest Fix #728 (#809)
          AUTO_CAST can be enabled on instance (#811)
          Ensure pyminify is on release script
          Add missing tomllib to monify script

    Gaurav Talreja (1):
          Fix #807 Use client.auth.approle.login instead of client.auth_approle (#808)

    Jitendra Yejare (1):
          Fix #768 of kv property depreciation from client object (#769)

    Joren Retel (2):
          Feature/detect casting comb token from converters (#784)
          Adding documentation and example to makefile. (#791)

    João Gustavo A. Amorim (1):
          Add pyupgrade hook (#759)

    Kian-Meng Ang (1):
          Fix typos (#788)

    Lucas Limeira (1):
          Using filter_strategy in env_loader to fix #760 (#767)

    Nicholas Nadeau, Ph.D., P.Eng (1):
          fix: typo (#766)

    Oleksii Baranov (2):
          Bump codecov action version (#775)
          Fix cli init command for flask (#705) (#774)

    Pedro de Medeiros (1):
          documentation fixes (#771)

    The Gitter Badger (1):
          Add a Gitter chat badge to README.md (#776)

    Théo Melo (1):
          Fixing a typo on the readme file (#763)

    Vicente Marçal (1):
          docs(pt-br): Docs Translation to brazilian portugues. (#787)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug HIGH High Priority
Projects
None yet
Development

No branches or pull requests

2 participants