Patrick
Patrick

Reputation: 612

Flask: Cant initialize Marshmallow

I use Flask, SqlAlchemy and Marshmallow to build a REST API with Python.

I used pipenv install flask flask-sqlalchemy flask-marshmallow and marshmallow-sqlalchemy and it went fine.

Here are my dependency versions:

click==7.1.2
Flask==1.1.2
flask-marshmallow==0.12.0
Flask-SQLAlchemy==2.4.3
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
marshmallow==2.21.0
marshmallow-sqlalchemy==0.19.0
six==1.15.0
SQLAlchemy==1.3.17
Werkzeug==1.0.1

Now I run this code to run a basic server without any methods

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os

# Init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Init db
db = SQLAlchemy(app)
# Init ma
ma = Marshmallow(app) # This is where the error appears

# Run Server
if __name__ == '__main__':
    app.run(debug=True)

When I run this code I get the following exception:

 File "/Users/pro/PycharmProjects/EventPlannerAPI/app.py", line 15, in <module>
    ma = Marshmallow(app)
  File "/Users/pro/.local/share/virtualenvs/EventPlannerAPI-OoqXq5gv/lib/python3.7/site-packages/flask_marshmallow/__init__.py", line 104, in __init__
    self.init_app(app)
  File "/Users/pro/.local/share/virtualenvs/EventPlannerAPI-OoqXq5gv/lib/python3.7/site-packages/flask_marshmallow/__init__.py", line 116, in init_app
    self.SQLAlchemySchema.OPTIONS_CLASS.session = db.session
AttributeError: 'NoneType' object has no attribute 'OPTIONS_CLASS'

As I am following a (1-2 years old) tutorial, I do not know why it does not work, especially because in a similar project everything is working fine.

Upvotes: 1

Views: 1296

Answers (2)

Sid
Sid

Reputation: 2582

I was having the same issue. I was using python 2.7 virtual environment and when I installed marshmallow-sqlalchemy, it installed by default version 0.19.0. During fixing process, I changed it to Python 3 and then changed the version to 0.23.0 in dependency. Now it's working fine. 0.19.0 version was causing the problem. But I am not sure if python version change was also required.

Upvotes: 1

Patrick
Patrick

Reputation: 612

It was a compatibility problem between the different versions. It should be fixed soon.

Upvotes: 1

Related Questions