My friend mentioned that he had a python session on his desktop that he never dared to close, since the setup had been so much trouble.
Thinking that it would be great to have this more frequently, and more easily, I created a python file which gets loaded every time I start my python interpreter, pyprompt.py.
import sys
import os
def getProjectDir(d=os.getcwd()):
projects = os.path.join(os.environ['HOME'], "projects")
if os.path.dirname(d) in ['/home', '/']:
return None
elif projects == os.path.dirname(d):
if os.path.exists(os.path.join(d, "src/pylons/proj")):
return d
else:
return None
else:
return getProjectDir(os.path.dirname(d))
pd = getProjectDir()
if pd:
print "Detected %s project. Setting up database." %(os.path.basename(pd))
sys.path.append(os.path.join(pd, "src/pylons/proj"))
import proj
from proj.lib.helpers import setup_db
factory = setup_db()
dbcon = factory.getConnection()
Now I want this file to run every time the python interpreter is started.
export PYTHONSTARTUP="/home/jsimpson/bin/pyprompt.py"
So, what happens is, when the python interpreter starts, it checks the PYTHONSTARTUP variable for a python file to run. That executes the getProjectDir() function, which tests if my current working directory is in a project. (Our workflow includes many branches of the main project.) If I am in a project, it will import the files from that project and setup my interpreter, ready for some interactive work.