flaskr tutorial; can't import flaskr (initialize database)


I'm new to programming, and tried to work through the flask tutorial. http://flask.pocoo.org/docs/tutorial/

I'm stuck on this part (from the readme on github) when trying to run the app: https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/

Fire up a python shell and run this:

from flaskr import init_db; init_db()

I get this error when I try to run the command in python shell:

Import error: No module named flaskr

And I get this error when I try to run app locally:

OperationalError: unable to open database file

I've been looking for solution for several hours now, but to no avail. Any thoughts on what I could check? Thank you.

9/5/2013 5:39:01 PM

Accepted Answer

The simplest way to accomplish what you need is to fire up the Python shell in the same folder as you have flaskr:

# I'm assuming that python is available on the command line
$ cd path/to/flaskr
$ python

# Python then runs and you can import flaskr
>>> from flaskr import init_db; init_db()
>>> exit()

The trick is that when you run Python it only looks in a certain number of places for modules and packages - you can see which places by running:

>>> from sys import path
>>> for fp in path:
...     print fp

from the Python interpreter. If the path to flaskr is not in that list flaskr can't be imported. By default Python adds the directory it is started in to its search path (which is why we start Python in the directory that contains flaskr.)

Once you have run init_db you should be able to run the application and see everything working.

8/25/2012 1:19:50 AM

The thing that fixed it for me was changing

export FLASK_APP=flaskr


export FLASK_APP=flaskr.py

Taken from here

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow