Anna Naden
Anna Naden

Reputation: 207

In Hello World flask app, Flask cannot find the app

I am running the minimal Hello World app from the Flask documentation. The code is below. I get no module named "flask.hello." The traceback is also below. I would like to know how to direct the flask to resolve the location of the app. The name of the source file is hello.py. I set

FLASK_APP='hello.py' and PYTHONPATH="."

I start Flask with the command:

flask run --host=0.0.0.0 --port=3000

from flask import Flask
app=Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello world!"
Traceback (most recent call last):
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
ModuleNotFoundError: No module named 'flask.hello'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 338, in __call__
    self._flush_bg_loading_exception()
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 326, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 314, in _load_app
    self._load_unlocked()
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 250, in locate_app
    raise NoAppException('Could not import "{name}".'.format(name=module_name))
flask.cli.NoAppException: Could not import "flask.hello".

Upvotes: 0

Views: 816

Answers (3)

Anna Naden
Anna Naden

Reputation: 207

My working directory was called "flask" which apparently confused flask. When I renamed it to flask_dir, all works.

Upvotes: 1

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

Run it like this from the directory where you have hello.py:

FLASK_APP='hello.py' PYTHONPATH="." flask run --host=0.0.0.0 --port=3000

There's a typo in your code:

from flask import Flask
app=Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello world!'

Upvotes: 1

Atte
Atte

Reputation: 308

Change FLASK_APP='hello.py' to FLASK_APP='hello'

Upvotes: 0

Related Questions