'Python + Flask App runs from wrong folder after run by Pycharm on 127.0.0.1
Setup:
I have created and populated a sqllite database in Pycharm, running Python + Flask + SQLAlchemy.
The code works perfectly in the console, but when i run it, i get a:
OperationalError: (sqlite3.OperationalError) unable to open database file
Problem:
When i run print os.getcwd() in Chrome/Flask-Werkzeug:
C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\jre\jre\bin.
that is the wrong folder. The whole project, including the .db is in C:\Users\Finn\PycharmProjects.
Why is that and what do i do about it?
Code:
Base = declarative_base()
engine = create_engine("sqlite:///temp/database.db")
DBSession = sessionmaker(bind=engine)
session = DBSession()
**more Imports, DB-Setup & Classes**
class DBHandl():
@classmethod
def getData(self): # Return Personen + Adressen
q = session.query(Person, Address).filter(Person.id == Address.person_id).all()
return q
@app.route('/')
def hello_world():
q = DBHandl.getData()
return render_template("home.html", list=q)
Solution 1:[1]
Settings => Build,Execution,Deployment => Console => Python Console
Set the working directory to your intended folder.
Solution 2:[2]
In PyCharm, did you change the project preferences? If you go in File > Settings > Project: yourprojectname, you should be able to define the root of the project and everything
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Seçkin Sava?ç? |
| Solution 2 | nnaelle |
