[Python] sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None...

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am running a program from another person who are inconvenience ask for help from. The program is a website. Server end is written by python and flask (module, http://flask.pocoo.org/). The program has been successfully run on the server. What I need to do is modify something on it. Since the production server is not allowed for test, I tested it in development server locally via flask. However, I could not run even the original program. Below is from python.

    (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi


    Traceback (most recent call last): File "index.wsgi", line 6, in from app import app as application
    ​

    File "/home/kevin/python/public_html/app.py", line 27, in <module>
    app = create_app()

    File "/home/kevin/python/public_html/app.py", line 12, in create_app
    database.init_db()

    File "/home/kevin/python/public_html/database.py", line 24, in init_db
    Base.metadata.create_all(engine)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2793, in create_all
    tables=tables)
    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1478, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:

    File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1471, in _optional_conn_ctx_manager
    with self.contextual_connect() as conn:

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1661, in contextual_connect
    self.pool.connect(),

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 272, in connect
    return _ConnectionFairy(self).checkout()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 425, in __init__
    rec = self._connection_record = pool._do_get()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 857, in _do_get
    return self._create_connection()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 225, in _create_connection
    return _ConnectionRecord(self)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 318, in __init__
    self.connection = self.__connect()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 368, in __connect
    connection = self.__pool._creator()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 80, in connect
    return dialect.connect(*cargs, **cparams)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 283, in connect
    return self.dbapi.connect(*cargs, **cparams)

    sqlalchemy.exc.OperationalError: (OperationalError) unable to open database file None None


    In the config.py file


    LOGFILE = '/tmp/ate.log' DEBUG = True TESTING = True THREADED = True DATABASE_URI = 'sqlite:////tmp/ate.db' SECRET_KEY = os.urandom(24)
    ​

    Hence, I created a folder called "tmp" under my user and an empty file called "ate.db". Then, ran it again. It said


    IOError: [Errno 2] No such file or directory: '/home/kevin/log/ate.log'
    ​

    Then, I created the log folder and the log file. Run it, but nothing happened like


    (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$ python index.wsgi (venv)kevin@ubuntu:~/python/public_html$
    ​

    If it is successful, the website should be available on http://127.0.0.1:5000/. However, it did not work. Does anybody know why and how to solve it? The codes should be fine since it is now available online. The problem should be a local problem. Thank you so much for your help.

    The code of where the program is stuck

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

    engine = None
    db_session = None
    Base = declarative_base()


    def init_engine(uri, **kwards):
    global engine
    engine = create_engine(uri, **kwards)
    return engine


    def init_db():
    global db_session
    db_session = scoped_session(sessionmaker(bind=engine))
    # 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()
    import models
    Base.metadata.create_all(engine)

    Continue reading...

Compartilhe esta Página