In my flask app I recreate a sqlite database at every start.
For this I use code as shown on the official webpage
My project structure looks like this
project_dir/
|-README.md
`-app/
|-StubbyServer.py (contains the flask root)
|-schema.sql
`- (all the other files)
Now my StubbyServer.py
contains:
def get_db():
db = getattr(Flask, '_database', None)
if db is None:
db = Flask._database = sqlite3.connect(DATABASE)
with open('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
db.row_factory = sqlite3.Row
return db
If my working directory is /path/project_dir/app
the command python StubbyServer.py
works fine
If my working directory is /path/project_dir
the command python app/StubbyServer.py
fails with:
File "app/StubbyServer.py", line 43, in get_db
with open('schema.sql', mode='r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'schema.sql'
I know why this happens but I don't know how I can work around this.
I want my flask app to work fine independent from my current working dir, how can I achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…