Skip to content

flask.cli.NoAppException raised when .env/.flaskenv exists in parent directory and python-dotenv installed #3561

@greyli

Description

@greyli

Description

With this minimal application:

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, Flask!'

and this file structure:

- project
    - .env
    - hello
        - app.py

When executing flask run inside the hello folder, you will get a NoAppException error, although the app.py is present in the current work directory. By the current implementation, since .env is in the top-level directory, Flask will change the current work directory to the directory that contains the .env file, so it can't find the app.py.

Steps to reproduce:

$ git clone https://github.com/greyli/flask-env-test
$ cd flask-env-test
$ pip install -r requirements.txt  # or just pip install flask[dotenv]
$ cd hello
$ flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

I think it will be a bit confused when the user accidentally put a .env or .flaskenv in the top-level directory. Can we drop this change directory behavior (#3560)?

Environment

  • Python version: 3.6.8
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions