2

I have been learning flask/ python from this tutorial http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

This blog is pretty good and it explain very well. In the first tutorial, if you notice, he asks us to create a init.py file, a views.py file and a main run.py file.

root
/microblog
  /apps
    /__init__.py
    views.py

I created the two files. He asks us to create a run.py file and put it in the root file. then

chmod a+x run.py
./run.py

It says the file does not exist. If I,

python run.py

It says App module not defined. I cannot figure out the problem, I put the run.py file in all the files, it doesnt not work what so ever.

I will also include the code so that it would be easier to answer instead of going to the above link

init.py

from flask import Flask
app = Flask(__name__)
from app import views

views.py

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

run.py

#!flask/bin/python
from app import app
app.run(debug = True)

My questions:

  1. Where should I put the run.py file?

  2. Why are we creating different files? Why cant all of them be in one full file?

    init.py -->

    • he is importing flask which is normal. then assigning app = (name). why is that? Then why is he importing views from apps?

views.py -->

  1. from app import app? is app an existing module here or the one we just created? what does @app.route('/') or
    @app.route('/index')do?

Can some one put the code together and explain it?

1
  • Please bare the long questions. Help me here. Commented Sep 8, 2013 at 13:12

1 Answer 1

2

It says App module not defined

You misspelled package name: you have apps in your directory tree and you try to import app

Where should I put the run.py file?

Anywhere you want as long app is in PYTHONPATH. Or you can put it inside microblog directory.

he is importing flask which is normal. then assigning app = (name). why is that?

# Create reference to flask WSGI application object
app = Flask(__name__)

Why? Because you need application to run. See official docs: Flask object

Then why is he importing views from apps?

from app import views

means: From package named app import module named views

Naming convention could be probably different but if you don't see the difference you should probably spend more learning python basics before starting with more sophisticated stuff.

from app import app? is app an existing module here or the one we just created? what > does @app.route('/') or @app.route('/index')do?

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

Short answer: if app receives request for url '/' responds using function foo For more see official docs: add_url_rule and URL Route Registrations

Update

Why are we creating different files? Why cant all of them be in one full file?

Actually nothing stops you from putting everything inside one file but most of the time it is really bad idea. If you ask about reasoning in this specific example it is mostly about separating modules that have different responsibilities.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a ton zero323. And I am really sorry, I didnt type it properly here, it is 'app' and not apps. It is right, but it aint working still.
Is it located inside microblog directory? If not move it there and try again
yes it is. It is located in the microblog directory. I am still getting the error 'No such file or directory'. The run.py file is highlighted in green color.
Your app should like right now like this: bitbucket.org/zero323/flask-example/src You should also check github.com/miguelgrinberg/microblog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.