Search code examples
flask-sqlalchemyweb-deploymentpythonanywhere

unable to open database file on a hosting service pythonanywhere


I want to deploy my project on pythonanywhere. Error.log says that server or machine is unable to open my database. Everything works fine on my local machine. I watched a video of Pretty Printed from YouTube

This how I initialize in app.py. This what I got from error.log

db_session.global_init("db/data.sqlite")

this in db_session:

def global_init(db_file):
global __factory

if __factory:
    return

if not db_file or not db_file.strip():
    raise Exception("Необходимо указать файл базы данных.")

conn_str = f'sqlite:///{db_file.strip()}?check_same_thread=False'
print(f"Подключение к базе данных по адресу {conn_str}")

engine = sa.create_engine(conn_str, echo=False)
__factory = orm.sessionmaker(bind=engine)

from . import __all_models
SqlAlchemyBase.metadata.create_all(engine)

def create_session() -> Session:
    global __factory
    return __factory()

last thing is my wsgi.py:

import sys
path = '/home/r1chter/Chicken-beta'
if path not in sys.path:
    sys.path.append(path)
import os
from dotenv import load_dotenv
project_folder = os.path.expanduser(path)
load_dotenv(os.path.join(project_folder, '.env'))
import app # noqa
application = app.app()

Solution

  • Usually errors like this on PythonAnywhere are due to providing relative path instead of absolute path.