导入数据库错误。Flask+SQLalchemy+SQLite部署到pythonanwhere失败

2024-10-01 11:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

未能将flask应用程序部署到python anywhere。跟随他们的教程https://help.pythonanywhere.com/pages/Flask/

在应用程序副本位于路径home/koti1/evo/app.py,数据库.py, 模型.py所有其他文件也在那里。在本地主机上一切正常。在

Python的设置:

 Source code:
/home/kotik1/evo

Working directory:
/home/kotik1/evo

WSGI配置文件:

^{pr2}$

日志中的错误:

:Error running WSGI application
:ImportError: No module named 'database'
:  File "/var/www/kotik1_pythonanywhere_com_wsgi.py", line 7, in <module>
:    from app import app as application
:
:  File "./app/app.py", line 6, in <module>
:    from database import db_session

同样,本地主机上的sqlite数据库没有问题。在

有数据库.py包含以下内容的文件:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    #gitcommen
    import models
    Base.metadata.create_all(bind=engine)

正如您从日志中看到的,init_db来自database.pyapp.py中被文件末尾的这些行调用。公司名称:

if __name__ == '__main__':
init_db()
app.run()

错误在导入行中应用程序副本但我无法找出原因,因为在本地主机上一切正常,并且安装了所有依赖项,如sqlalhemy。原因可能是什么?在

from database import db_session

Tags: 文件frompyimport数据库app应用程序home