[Flask] help getting hello world package-based webapp running without installing it

John Gabriele jgabriele at fastmail.fm
Thu Jul 20 12:14:09 EDT 2017


I'd like to get a simple hello-world Flask webapp running --- without
installing it yet --- but am having trouble. This is a package-based
webapp, as outlined in the Flask tutorial (like flaskr).

I've created:

my-proj/
    my-env/  # a venv with flask installed
    README.txt
    my_webapp/
        __init__.py  # empty
        my_webapp.py
        static/
            style.css
        templates/

my_webapp.py looks like:

~~~
from flask import Flask

app = Flask('my_webapp')

@app.route('/')
def index():
    return "Hi"
~~~

I have the venv activated.

My relevant environment variables are set:

    FLASK_APP=my_webapp.my_webapp
    FLASK_DEBUG=true

I was hoping to get this simplest case working, without adding anything
to __init__.py, and without installing my webapp package. I'd like to
run it on its dev server while developing. I can install/deploy later.
It's my understanding that this is the customary way to develop locally
with Flask.

In my-proj directory, and with my venv activated, if I try `flask run`,
it will run,

~~~
(my-env) my-proj$ flask run
 * Serving Flask app "my_webapp.my_webapp"
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 924-462-254
~~~

but then when I access localhost:5000/ (which strips that last slash
after hit Enter), I get:

| flask.cli.NoAppException: The file/path provided (my_webapp.my_webapp)
does
| not appear to exist. Please verify the path is correct. If app is not
on PYTHONPATH,
| ensure the extension is .py

One thing that *does* work: if I instead run via `python -m flask run`,
I get

~~~
(my-env) my-proj$ python -m flask run
 * Serving Flask app "my_webapp.my_webapp"
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/usr/lib/python3.5/runpy.py:125: RuntimeWarning: 'flask.cli' found in
sys.modules after import of package 'flask', but prior to execution of
'flask.cli'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
 * Debugger is active!
 * Debugger PIN: 152-420-413
~~~

and I can load the page and it works. Why does that work but not plain
`flask run`?

Is something wrong in my `app = Flask(...)` arg, or in my env vars?

Is this the customary way to set up a Flask webapp (as a package) and
run it from the project directory?

Thanks!
-- John


More information about the Flask mailing list