20
votes

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)
8
is there meant to be 4 //// after sqlite: or just 3? Also /tmp/ate.db means it is stored in /tmp and not tmp. Unless you are chroot'ed of course. - Joe Doherty
It meant 4. I think it is the problem of my local environment because it can be run in the other computer and server. However, I do not know what do I miss in my laptop. - user2677756
It may be worth providing the code around where the stack trace points to - Joe Doherty
Hi @JoeDoherty , I have attached the code in the question. Thank you - user2677756

8 Answers

34
votes

Replace:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////dbdir/test.db'

With:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dbdir/test.db'
15
votes

finally figured it out, had help tho

 import os

file_path = os.path.abspath(os.getcwd())+"\database.db"

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path
 db = SQLAlchemy(app)
8
votes

I had this issue with sqlite. The process trying to open the database file needs to have write access to the directory as it creates temporary/lock files.

The following structure worked for me to allow www-data to use the database.

%> ls -l
drwxrwxr-x  2 fmlheureux www-data     4096 Feb 17 13:24 database-dir

%> ls -l database-dir/
-rw-rw-r-- 1 fmlheureux www-data 40960 Feb 17 13:28 database.sqlite
5
votes

My database URI started rocking after adding one dot in between ////. Working on windows 7. I had directory and db-file created prior to calling this.

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./dbdir/test.db'
1
votes

I think I've seen errors like this where file permissions were wrong for the .db file or its parent directory. You might make sure that the process trying to access the database can do so by appropriate use of chown or chmod.

This is specifically about Django, but maybe still relevant: https://serverfault.com/questions/57596/why-do-i-get-sqlite-error-unable-to-open-database-file

1
votes

I just met this same problem and found that I make a stupid circular reference .

./data_model.py

from flask.ext.sqlalchemy import SQLAlchemy
from api.src.app import app

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////database/user.db')

db = SQLAlchemy(app)

./app.py

...
from api.src.data_model import db
db.init_app(app)

Then I removed the app.py/db and it works.

0
votes

You're not managing to find the path to the database from your current level. What you need to do is the following:

DATABASE_URI = 'sqlite:///../tmp/ate.db'

That means go up to the root level .. and then navigate down to the database (the relative path is /tmp/ate.db in this case).

0
votes

For those looking for a solution to the OperationalError, not necessarily caused by being unable to open database file None None - you might try adding a pool_pre_ping=True argument to create_engine, i.e.

engine = create_engine("mysql+pymysql://user:pw@host/db", pool_pre_ping=True)

see sqlalchemy documentation:

Pessimistic testing of connections upon checkout is achievable by using the Pool.pre_ping argument, available from create_engine() via the create_engine.pool_pre_ping argument

The “pre ping” feature will normally emit SQL equivalent to “SELECT 1” each time a connection is checked out from the pool; if an error is raised that is detected as a “disconnect” situation, the connection will be immediately recycled, and all other pooled connections older than the current time are invalidated, so that the next time they are checked out, they will also be recycled before use.