Problems with Flask and PyCharm

Problems with Flask and PyCharm

I started a new Flask project and coded it in VisualCode and a Terminal. Therefore, I set up my virtual environment, installed Flask and started to code after the Flask Quistart.

The code looks like this:

from flask import Flask
app = Flask( __name__ )

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

Then I ran the app with:

$ export FLASK_APP=hello.py
$ flask run
 * Running on http://127.0.0.1:5000/

Because of the great support for Web Apps I decided to switch to PyCharm. I imported the project into PyCharm, set the Interpreter to the virtualenv, but when it hit run nothing happend….

If you have the same problem: Feel welcome, I’ve got the solution!

Create a New Project, choose the Flask Template and select your existing flask project folder. Say yes to the pop-up which asks you to create a project from existing source.

Your project should open now and you can change your interpreter in the settings to your virtualenv or whatever you desire.

If you try to run the app now you should see something like this:

I tried everything but the solution is damn simple… add this at the end of your code:

if __name__ == ' __main__':
    app.run()

and your console should output * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit).

If you want to know more about the if __name__ == ' __main__': line I can recommend this Video from Corey Schafer.

Thank you for reading!

06.09.2017


KurzGedanke

KurzGedanke

Flexibel, belastbar, innovativ, kreativ, teamfähig, begeisterungsfähig und kreativ. 🏳️‍🌈Pronoun: Es/It🏳️‍🌈