What does if __name__ == "__main__"
mean?
The conditional check if __name__ == "__main__"
is used to determine if the script being executed is the main program file (in this case, app.py
).
For example, suppose you have a script named utils.py
with a single line print(__name__)
. When you import utils.py
into another file, say app.py
, and execute app.py
file, the print statement in utils.py
will display “utils,” which is the file’s name.
However, if you run app.py
directly, the __name__
variable for app.py
will be set to "__main__"
. This allows if __name__ == "__main__":
to act as a safeguard, running certain code (like app.run()
) only when app.py
is executed as the main program and not when it’s imported as a module in another script.
Step 3: Run your Flask 'Hello, World" application
Now that you have created your Flask application, it’s time to run it and see “Hello, World!” displayed in your browser. Click the “Run” button below and do the following in the terminal:
Ensure that Flask
is installed in your virtual environment: