From 3c049ad734c887fda89bbe0ef17a6c697cca592d Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Sun, 18 Jun 2017 13:32:29 -0700 Subject: [PATCH] #2377: Don't require FLASK_APP to have .py extension --- CHANGES | 3 +++ flask/cli.py | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index f0cb97232d..041d23d24f 100644 --- a/CHANGES +++ b/CHANGES @@ -82,6 +82,8 @@ Major release, unreleased - Fix incorrect JSON encoding of aware, non-UTC datetimes. (`#2374`_) - Template auto reloading will honor the ``run`` command's ``debug`` flag even if ``app.jinja_env`` was already accessed. (`#2373`_) +- The ``flask`` command no longer requires that the ``FLASK_APP`` environment + variable have a ``.py`` extension. (`#2383`_) .. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1621: https://github.com/pallets/flask/pull/1621 @@ -107,6 +109,7 @@ Major release, unreleased .. _#2362: https://github.com/pallets/flask/pull/2362 .. _#2374: https://github.com/pallets/flask/pull/2374 .. _#2373: https://github.com/pallets/flask/pull/2373 +.. _#2383: https://github.com/pallets/flask/pull/2383 Version 0.12.2 -------------- diff --git a/flask/cli.py b/flask/cli.py index 536f53ee1d..6170fa2c40 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -177,6 +177,14 @@ def prepare_exec_for_file(filename): return '.'.join(module[::-1]) +def prepare_exec_for_dir(dirpath): + """Given a directory this will put the parent directory on the python path + and return the directory name. Used for when FLASK_APP is a module that + contains an app + """ + sys.path.append(os.path.join(dirpath, os.pardir)) + return os.path.basename(dirpath) + def locate_app(script_info, app_id, raise_if_not_found=True): """Attempts to locate the application.""" @@ -222,8 +230,11 @@ def find_default_import_path(): app = os.environ.get('FLASK_APP') if app is None: return - if os.path.isfile(app): - return prepare_exec_for_file(app) + if os.path.isdir(app): + return prepare_exec_for_dir(app) + for path in [app, app + '.py']: + if os.path.isfile(path): + return prepare_exec_for_file(path) return app