Flask application fails to read data from SQLite database on server using pd.read_sql

Question:

In my flask application, I have a model class called Well, which includes a function called getProdDF, which pulls production data from a model class called ProductionData and turns it into a pandas dataframe using pd.read_sql. On my local machine it works fine like this:

class Well:
    id = db.Column(db.Integer, primary_key=True)
    production = db.relationship('ProductionData', backref='well', lazy='dynamic')
    
    def getProdDF(self): 
        data = self.production
        df = pd.read_sql(data.statement, data.session.bind)

class ProductionData(ProdData, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    well_id = db.Column(db.Integer, db.ForeignKey('well.id'))
    oil = db.Column(db.Float)
    #etc

but when I tried to move the code to a server and try to run it there it gives me this error:

  File "C:UsersusrCodeProjNameappmodels.py", line 517, in getProdDF
    df = pd.read_sql(data.statement, data.session.bind)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameLibsite-packagespandasiosql.py", line 564, in read_sql
    return pandas_sql.read_query(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameLibsite-packagespandasiosql.py", line 2078, in read_query
    cursor = self.execute(*args)
             ^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameLibsite-packagespandasiosql.py", line 2016, in execute
    cur = self.con.cursor()
          ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'cursor'

Does anyone know what I’m doing wrong? The database file (app.db) is copied over, I can connect to the database and get data from it in the command line, but I get the same error when I try to run the Well model’s getProdDF function:

>>> from app import app, db
>>> from app.models import Well
>>> app.app_context().push()
>>> w = Well.query.first()
>>> w.getProdDF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:UsersusrCodeProjNameappmodels.py", line 517, in getProdDF
    df = pd.read_sql(data.statement, data.session.bind)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameLibsite-packagespandasiosql.py", line 564, in read_sql
    return pandas_sql.read_query(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameemphasized textLibsite-packagespandasiosql.py", line 2078, in read_query
    cursor = self.execute(*args)
             ^^^^^^^^^^^^^^^^^^^
  File "C:UsersusrEnvsProjNameLibsite-packagespandasiosql.py", line 2016, in execute
    cur = self.con.cursor()
          ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'cursor'

Thanks a lot,
Alex

Asked By: Alex S

||

Answers:

After a lot of frustration, I found the problem. On my local machine I had SQLAlchemy==1.4.29 and Flask-SQLAlchemy==2.5.1 installed, while on the server I had SQLAlchemy==1.4.46 and Flask-SQLAlchemy==3.0.3 installed. For whatever reason, the new versions of these packages didn’t work on my site.

To fix it I first uninstalled the new versions on the server:

>>> pip uninstall SQLAlchemy
>>> pip uninstall Flask-SQLAlchemy

Then installed the old versions by specifying the version in pip like this:

>>> pip install SQLAlchemy==1.4.29
>>> pip install Flask-SQLAlchemy==2.5.1

And that fixed it.

Thanks,
Alex

Answered By: Alex S
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.