Import app from wsgi.py or app.py if FLASK_APP is not defined
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #2376
authored by Miguel Grinberg · committed by Miguel Grinberg · parent d625d41 · Browse files · 4 files·+48 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+48 added−10 removed
- tests:+28 added−1 removed
- other:+20 added−9 removed
Provenance
- Commit
7c40aa9- Tree
6946f08- Author
Miguel Grinberg <miguel.grinberg@gmail.com>- Committer
Miguel Grinberg <miguel.grinberg@gmail.com>- Parent
d625d41
4 files changed+48 added−10 removed
modified:flask/cli.py+20 added-9 removed
170 unmodified lines171 module.append(extra)172 if not os.path.isfile(os.path.join(dirpath, '__init__.py')):173 break174 175+ if sys.path[0] != dirpath:175- sys.path.insert(0, dirpath)176+ sys.path.insert(0, dirpath)177 return '.'.join(module[::-1])178 179 179-def locate_app(script_info, app_id):180+def locate_app(script_info, app_id, raise_if_not_found=True):181 """Attempts to locate the application."""182 __traceback_hide__ = True183 if ':' in app_id:184 module, app_obj = app_id.split(':', 1)8 unmodified lines193 # Determine this by checking whether the trace has a depth > 1.194 if sys.exc_info()[-1].tb_next:195 stack_trace = traceback.format_exc()196 raise NoAppException('There was an error trying to import'196- ' the app (%s):\n%s' % (module, stack_trace))197- else:197+ ' the app (%s):\n%s' % (module, stack_trace))198+ elif raise_if_not_found:199 raise NoAppException('The file/path provided (%s) does not appear'200 ' to exist. Please verify the path is '201 'correct. If app is not on PYTHONPATH, '202 'ensure the extension is .py' % module)203+ else:204+ return205 206 mod = sys.modules[module]207 if app_obj is None:208 return find_best_app(script_info, mod)119 unmodified lines328 329 if self.create_app is not None:330 rv = call_factory(self.create_app, self)331 else:329- if not self.app_import_path:332+ if self.app_import_path:333+ rv = locate_app(self, self.app_import_path)334+ else:335+ for module in ['wsgi.py', 'app.py']:336+ import_path = prepare_exec_for_file(module)337+ rv = locate_app(self, import_path,338+ raise_if_not_found=False)339+ if rv:340+ break341+ if not rv:342 raise NoAppException(343 'Could not locate Flask application. You did not provide '332- 'the FLASK_APP environment variable.\n\nFor more '333- 'information see '344+ 'the FLASK_APP environment variable, and a wsgi.py or '345+ 'app.py module was not found in the current directory.\n\n'346+ 'For more information see '347 'http://flask.pocoo.org/docs/latest/quickstart/')348 336- rv = locate_app(self, self.app_import_path)337-349 debug = get_debug_flag()350 351 if debug is not None:352 rv._reconfigure_for_run_debug(debug)335 unmodified linesadded:tests/test_apps/helloworld/hello.py+6 added
1+from flask import Flask2+app = Flask(__name__)3+4+@app.route("/")5+def hello():6+ return "Hello World!"added:tests/test_apps/helloworld/wsgi.py+1 added
1+from hello import appmodified:tests/test_cli.py+21 added-1 removed
179 unmodified lines180 NoAppException, locate_app,181 script_info, "cliapp.factory:create_app ()")182 pytest.raises(183 NoAppException, locate_app, script_info, "cliapp.importerrorapp")184+ assert locate_app(script_info, "notanpp.py",185+ raise_if_not_found=False) is None186 187 188 def test_find_default_import_path(test_apps, monkeypatch, tmpdir):189 """Test of find_default_import_path."""25 unmodified lines215 assert flask_ver in out216 assert py_ver in out217 218 217-def test_scriptinfo(test_apps):219+def test_scriptinfo(test_apps, monkeypatch):220 """Test of ScriptInfo."""221 obj = ScriptInfo(app_import_path="cliapp.app:testapp")222 assert obj.load_app().name == "testapp"223 assert obj.load_app().name == "testapp"5 unmodified lines229 app = obj.load_app()230 assert app.name == "createapp"231 assert obj.load_app() == app232 233+ obj = ScriptInfo()234+ pytest.raises(235+ NoAppException, obj.load_app)236 237+ # import app from wsgi.py in current directory238+ monkeypatch.chdir(os.path.abspath(239+ os.path.join(os.path.dirname(__file__), 'test_apps', 'helloworld')))240+ obj = ScriptInfo()241+ app = obj.load_app()242+ assert app.name == 'hello'243+244+ # import app from app.py in current directory245+ monkeypatch.chdir(os.path.abspath(246+ os.path.join(os.path.dirname(__file__), 'test_apps', 'cliapp')))247+ obj = ScriptInfo()248+ app = obj.load_app()249+ assert app.name == 'testapp'250+251+252 def test_with_appcontext(runner):253 """Test of with_appcontext."""254 255 @click.command()133 unmodified lines