Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for the python interpreative console to import my entire app at one time?

Tags:

python

flask

Is there some sort of method or resource I can reference that would allow me to import my entire application into the python interpreter?

For example when I run python to do some flask-sql queries I have to do this EVERYTIME I exit:

python
import project
from project import app,db, etc etc
from project.models import Model, Model,
 goes on and on......

How can I avoid doing that to bypass the repetiveness? Coming from Rails, its so nice to just run rails c and have everything loaded for you.

like image 523
Dan Rubio Avatar asked Dec 14 '22 09:12

Dan Rubio


1 Answers

You can have Python execute a file on interactive startup by setting the environmental variable PYTHONSTARTUP:

$ cat file.py 
print 'These definitions are executed before the REPL'
pi = 3.14
$ PYTHONSTARTUP=file.py python
Python 2.7.3 (default, Mar 21 2013, 07:25:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
These definitions are executed before the REPL
>>> pi
3.14
>>> 

It's right there at the bottom of python --help.

like image 148
Dan D. Avatar answered May 04 '23 22:05

Dan D.