FlaskBook Tutorial Set up questions — can I work "from scratch"?


I'm new to Python and Flask, and PythonAnywhere seems to be a great sandbox to learn within. Thanks for setting it up.

I'm attempting to follow Miguel Grinburg's Flask Book tutorial here (which is also an O'Reilly Book). I'm comfortable on the command line (I am a working UX designer who codes, just inexperienced with programming) and I wanted to follow his instructions on installing Flask through the CLI, one for the practice, but two, to ensure my install and his example install matched.

Is it possible for me to configure my PA account so that I can simply open your bash console and follow along with his directions, which include setting up virtualenv manually? I figure it would be good practice to do it from scratch. (I don't need help with the doing, just how to get PA set up so I can start at step 1 of his tutorial)

FWIW, I tried a few times, but quickly got myself stuck, then I tried to search the forums—I see I'm not the first to try Miguel's tutorial here… but their questions didn't really address the "can/how do I just set things up from scratch from the shell?"

Many thanks. ~brian

You should be able to create the virtualenv as described in the tutorial from a bash console. However, there are a few important differences:

  • On PythonAnywhere, instead of creating the virtualenv using one of the methods described in the tutorial, use

    mkvirtualenv --python=which python3.4 <virtualenv name>

  • When the tutorial says to create a script, that won't work on PythonAnywhere. You'll need to create a web app and point it at the code that you wrote and at the virtualenv that you created. There are instructions in the help pages. Look for how to create a web app with manual configuration.

Thank you, Glenn. I'll give it a shot. I appreciate the tips.

Alright, I think I get the concept of how to do this, but I'm not sure I have all the filepaths figured out to run a creation. I was able to install everything, though I'm not 100% sure if I placed things in the proper hierarchy. I know the stuff within the app dir are fine. But I'm not sure if the app dir itself is in the right place, in relativity to the virtualenv and where pip installed things. If the tutorial had a file tree I could look out, I'd be able to figure it out. (alas…)

If I can figure out what to point where, I think I can get the WSGI configured. But this is where I'm hitting a wall (spent about 2hrs trying different things this morning). Any other Python Anywhere around the forum ever run Miguel's tutorial? If I could just figure out what maps to what for the first example (link above), I think I'd be good for the rest of the tutorial.

This is what my WSGI looks like right now, throwing two errors

[edit: unfortunately, the forum isn't importing my image properly. So here's a straight link.]

I'm pretty sure the 2nd "word" on line 40 is the most egregious problem, but I know nothing about line 33, either. I've read the error logs, but at this point in my learning, they're effectively Greek to me.

Anyone out there gone this route before me?

Thanks, ~brian

Hi Brian,

I would suggest looking at your error log at /var/log/ (you can also access it from your webapps page under the logfiles section).

If you look at the most recent error (at the bottom), you will see that it can't import the module "app".

One note about the import error. from Flask import app means that there should be a Flask folder with an file inside of it.

Also, note that you don't actually need to (and you also should not) put the source code (ie. your web app) inside of your virtualenv folder.

Hello again.

I am at an impasse. I'm sure whatever is amiss is a simple thing that has to do with me not knowing which wires to plug to which. I went back and on your advice moved everything out of my virtualenv and tried to better replicate Miguel's setup in the tutorial. (The "Flask" folder is of his design) I figure I'll give it one last try, lay all of my files bare, and someone might spot the obvious mistake:

Virtual Environment lives here:


I don't know where to look for the Flask modules I installed with pip, though. I did not see them in bin in this dir, though, it could well not be the right place to look!

Dir structure

    - static
    - templates
    - file contents:

from flask import Flask

app = Flask(__name__)
from app import views

end init file contents:

from app import app

def index():
    return "Hello, World!(BD1)"

end views

Start WSGI config file

import os #(error read) os imported but unused
import sys

path = '/home/briandigital/microblog/Flask/app'
if path not in sys.path:

from app import app as application #(error read) application imported but unused

End WSGI config file

Start -- trimmed to show one call to index.

2015-09-07 13:59:06,057 :/usr/lib/python2.7/ RuntimeWarning: tp_compare didn't return -1 or -2 for exception
2015-09-07 13:59:06,058 :  return _active[_get_ident()]
2015-09-07 13:59:06,058 :Traceback (most recent call last):
2015-09-07 13:59:06,058 :  File "/bin/", line 134, in __call__
2015-09-07 13:59:06,058 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-09-07 13:59:06,058 :  File "/usr/lib/python2.7/logging/", line 1185, in exception
2015-09-07 13:59:06,059 :    self.error(msg, *args, **kwargs)
2015-09-07 13:59:06,059 :  File "/usr/lib/python2.7/logging/", line 1178, in error
2015-09-07 13:59:06,059 :    self._log(ERROR, msg, args, **kwargs)
2015-09-07 13:59:06,059 :  File "/usr/lib/python2.7/logging/", line 1270, in _log
2015-09-07 13:59:06,059 :    record = self.makeRecord(, level, fn, lno, msg, args, exc_info, func, extra)
2015-09-07 13:59:06,059 :  File "/usr/lib/python2.7/logging/", line 1244, in makeRecord
2015-09-07 13:59:06,060 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-09-07 13:59:06,060 :  File "/usr/lib/python2.7/logging/", line 284, in __init__
2015-09-07 13:59:06,060 :    self.threadName = threading.current_thread().name
2015-09-07 13:59:06,060 :  File "/usr/lib/python2.7/", line 1160, in currentThread
2015-09-07 13:59:06,060 :    return _active[_get_ident()]
2015-09-07 13:59:06,060 :  File "/bin/", line 126, in __call__
2015-09-07 13:59:06,060 :    app_iterator =, start_response)
2015-09-07 13:59:06,061 :  File "/bin/", line 140, in import_error_application
2015-09-07 13:59:06,061 :    raise e
2015-09-07 13:59:06,061 :ImportError: No module named app
2015-09-07 13:59:36,580 :Traceback (most recent call last):
2015-09-07 13:59:36,580 :  File "/bin/", line 134, in __call__
2015-09-07 13:59:36,581 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-09-07 13:59:36,581 :  File "/usr/lib/python2.7/logging/", line 1185, in exception
2015-09-07 13:59:36,581 :    self.error(msg, *args, **kwargs)
2015-09-07 13:59:36,581 :  File "/usr/lib/python2.7/logging/", line 1178, in error
2015-09-07 13:59:36,581 :    self._log(ERROR, msg, args, **kwargs)
2015-09-07 13:59:36,581 :  File "/usr/lib/python2.7/logging/", line 1270, in _log
2015-09-07 13:59:36,582 :    record = self.makeRecord(, level, fn, lno, msg, args, exc_info, func, extra)
2015-09-07 13:59:36,582 :  File "/usr/lib/python2.7/logging/", line 1244, in makeRecord
2015-09-07 13:59:36,582 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-09-07 13:59:36,582 :  File "/usr/lib/python2.7/logging/", line 284, in __init__
2015-09-07 13:59:36,582 :    self.threadName = threading.current_thread().name
2015-09-07 13:59:36,582 :  File "/usr/lib/python2.7/", line 1160, in currentThread
2015-09-07 13:59:36,583 :    return _active[_get_ident()]
2015-09-07 13:59:36,583 :  File "/bin/", line 126, in __call__
2015-09-07 13:59:36,583 :    app_iterator =, start_response)
2015-09-07 13:59:36,583 :  File "/bin/", line 140, in import_error_application
2015-09-07 13:59:36,583 :    raise e
2015-09-07 13:59:36,583 :ImportError: No module named app


I hope the problem is obvious to experts, because if not, I guess I have to give up on PA and instead this tutorial locally, and I was really hoping to learn in this lovely environment here.

Thank you, and sorry to be a hassle.

I think the problem is that you've got two things called "app" there. There's the module -- that is, the directory ~/microblog/Flask/app/, which contains an file -- and there's the variable inside that file.

You can see the problem here:

app = Flask(__name__)
from app import views

You've defined a variable called app on the first line and then you're trying to import from the module on the next line.

I suggest you rename one of them; either change the directory from being ~/microblog/Flask/app/ to something like ~/microblog/Flask/mysite/ and change all of your imports to use the new module name, or rename the variable from app to, say, application, and then change all of the @app.route decorators and the line that imports it in the WSGI file.

Does that make sense?

Thanks for the reply, Giles. Your response makes plenty of sense… multiple things named "app" certainly is confusing to me… but I didn't write these files… I merely copied them verbatim from the tutorial. If I start changing names, then I can't follow along in the tutorial… If you missed it at the top of the thread, I'm attempting to follow this tutorial: —which normally, if I came across some write up on the 'net that said start using the same word for all these different things, I'd be suspicious. But since it has since been released as an O'Reilly book, I presume they used their normal technical editors on it, and Miguel's trying to teach me something by using the same word for two things… but what do I know? I'm a total n00b at this. I mean… from app import app makes my head explode a little, too…

I genuinely appreciate all you guys on staff trying to give me a hand here… you've been very kind.

The "two things called app" thing sounds like something to raise with the author of the tutorial.

I think your actual issue isn't aliased apps, if I'm reading your traceback and configuration correctly, the line

path = '/home/briandigital/microblog/Flask/app'

should be

path = '/home/briandigital/microblog/Flask'

I'm back again.

I made change Glenn suggested above. I'm still getting errors in the WSGI file.

import os # error: os imported but not used
import sys

path = '/home/briandigital/microblog/Flask'
if path not in sys.path:

from app import app as application # error: application imported but not used

…and of course my site doesn't load in a browser.

My assumption to this point is that application was what is used on PA's end to execute the Flask app. I don't have any info on my end otherwise of how to set that. If it possible that should be something other than application?

You can ignore those two warnings, although if you're feeling really picky, you could delete the "import os" line. They're more like suggestions than pointers to actual problems with the code.

The actual problem with your app will be elsewhere. Is there anything in your error log? You'll find a link to it from the web tab...

Here is the fix that worked for me. At least through section 4 (Database). I toyed with in multiple ways up to this point. Then started from the WSGI file and exact copies of the code files. Finally found something that worked.

Definitely don't use the file. That is a given.

Set up your file with the ~/microblog and the ~/microblog/app directories in the sys.path. Plus 'import app as application' 'from init'.

# add your project directory to the sys.path
project_home = u'/home/username/mysite/microblog'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path
# adding project working directory to sys.path (added by Dan Carroll)
project_work = u'/home/username/mysite/microblog/app'
if project_work not in sys.path:
    sys.path = [project_work] + sys.path

# import flask app but need to call it "application" for WSGI to work
from __init__ import app as application

The file is fine as presented. Even the 'from app import views, models'.

in the file, change 'from app import app' to 'from init import app'.

from flask import render_template, flash, redirect
from __init__ import app
from forms import LoginForm

in the file everything is fine. No changes required here.

That should do it. At least if worked for me.

from microblog import app as application

This simple code in WSGI is fixed my problem .