-2

I just installed Python, pip, and flask. Then I created a test file (hello.py):

from flask import Flask
app = Flask(_name_)
@app.route('/')
def index():
   return 'Hello world!'
if __name__ == "__main__":
app.run()

when I try to run the file using

>set FLASK_APP=hello.py

>flask run

I get this error

 Serving Flask app "hello.py"
 * 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
Traceback (most recent call last):
  File "c:\users\ahmed\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\ahmed\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\ahmed\helloworld\env\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 305, in __init__
    self._load_unlocked()
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "c:\users\ahmed\helloworld\env\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "C:\Users\ahmed\helloworld\hello.py", line 2, in <module>
    app = Flask(_name_)
NameError: name '_name_' is not defined
Ahmed
  • 7
  • 3

1 Answers1

0

The key is here

File "C:\Users\ahmed\helloworld\hello.py", line 2, in <module>
    app = Flask(_name_)
NameError: name '_name_' is not defined

So, change

app = Flask(_name_)

to

app = Flask(__name__)

The reason for the double underscore is that in Python they have a special meaning. See Underscore vs Double underscore with variables and methods and What is the meaning of single and double underscore before an object name?

The code you posted has also other issues. For example, in the last line you have no identation. Also, your formatting can be improved. Putting it together, your code might look like this.

from flask import Flask


app = Flask(__name__)


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


if __name__ == "__main__":
    app.run()
MacOS
  • 1,149
  • 1
  • 7
  • 14