Python + Flask应用程序在127.0.0.1上由Pycharm运行后从错误的文件夹运行 [英] Python + Flask App runs from wrong folder after run by Pycharm on 127.0.0.1

查看:1928
本文介绍了Python + Flask应用程序在127.0.0.1上由Pycharm运行后从错误的文件夹运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

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

lockquote

OperationalError:(sqlite3.OperationalError)无法打开数据库文件



问题:



<在Chrome / Flask-Werkzeug中运行 print os.getcwd()

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

即错误的资料夹。整个项目,包括 .db C:\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
为什么是这样,我该怎么办呢?

代码:

$ p $ Base = declarative_base()
engine = create_engine(sqlite:///temp/database.db)
DBSession = sessionmaker(bind = engine)
session = DBSession()

** more进口,DB-Setup&类**

类DBHandl():
@classmethod
get getData(self):#返回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)


解决方案

非常感谢你的回答!他们带领我去找出什么事情和你们在哪里非常接近!
我必须改变运行/调试配置,当你点击我的项目右上角和编辑配置
那里我改变了工作目录


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)

解决方案

Thank you both so much for your answers! They led me on the path to figuring out what was up and you where very close! I had to change theRun/Debug Configurations that open up when you click on my Project in the upper right corner and edit configurations. there i changed the working directory

这篇关于Python + Flask应用程序在127.0.0.1上由Pycharm运行后从错误的文件夹运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆