Skip to main content
 首页 » 编程设计

Python + Flask 应用程序在 Pycharm 在 127.0.0.1 上运行后从错误的文件夹运行

2025年05月04日12duanxz

设置:

我在 Pycharm 中创建并填充了一个 sqllite 数据库,运行 Python + Flask + SQLAlchemy.
该代码在控制台中运行完美,但是当我运行它时,我得到:

OperationalError: (sqlite3.OperationalError) unable to open database file

问题:

当我在 Chrome/Flask-Werkzeug 中运行 print os.getcwd() 时:

C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\jre\jre\bin.

那是错误的文件夹。整个项目,包括 .db 都在 C:\Users\Finn\PycharmProjects 中。 为什么会这样,我该怎么办?

代码:

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) 

请您参考如下方法:

Settings => Build,Execution,Deployment => Console => Python Console

将工作目录设置为您想要的文件夹。