The documentation recommends putting imports in a module and import that when you're in an interactive session. Even better, I think, is to make an executable that launches a preconfigured Python shell.
Put something like this in for example shell.py and run chmod +x shell.py.
#!/usr/bin/env python
import os
import readline
from pprint import pprint
from flask import *
from myapp import *
from utils import *
from db import *
from models import *
os.environ['PYTHONINSPECT'] = 'True'
Normally import * should be avoided but unless you get namespace collisions it makes sense for an interactive shell environment.
Now you can simply do ./shell.py.
This snippet by Dag Odenhall can be used freely for anything you like. Consider it public domain.
Comments
Using werkzeug.script by Dan Jacob on 2010-05-28 @ 07:16
Another way to create a ready shell is to use the werkzeug.script library detailed here:
http://werkzeug.pocoo.org/documentation/0.6.1/script.html
For my Flask project I have also have a shell.py script:
This will use IPython automatically if installed.
The make_shell function passes a dict of variables you want included automatically in your shell - in this case the app instance, models module and SQLAlchemy session and metadata (as most of the time I will need to access the database).
werkzeug.script by Dan Jacob on 2010-05-28 @ 08:02
Armin may be planning a rewrite of werkzeug.script, but until we know what that's going to look like it's still part of the Werkzeug package.
I would therefore use it and change the code as and when needed.
Re: Using werkzeug.script by Dag Odenhall on 2010-05-28 @ 07:58
I used
werkzeug.scriptfirst but:Also
import *raises aSyntaxWarningif not used at the top-level, and I couldn't even suppress the exception.If however
werkzeug.scriptworks for you, there are of course benefits to it such as easily setting up arunservercommand, or any other custom commands.Flask-Script by Dag Odenhall on 2010-07-21 @ 00:54
Flask-Script is now an option that's looking good; although it might have the same issue with
import *.Simulating import * with Flask-Script by Dag Odenhall on 2010-08-09 @ 16:47
I solved the issue with
SyntaxWarning. The warnings module didn't help but I realised this rather obvious solution: