Search code examples
python-3.xflaskflask-sqlalchemy

TypeError: can't apply this __setattr__ to DefaultMeta object - Flask app - python


The initial problem, I am developing on the flask app framework and I was trying out blueprint template and it update my version of python I think to 3.8.4 and this new error appear. I was on python 3.8.3

flask run

outputted a complicated error a python type Error.

     File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1264.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1264.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code     
    exec(code, run_globals)
  File "D:\Openvessel\vessel-app\vessel_env\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 305, in __init__
    self._load_unlocked()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "D:\Openvessel\vessel-app\Back-end\app.py", line 1, in <module>
    from vessel_app import create_app
  File "D:\Openvessel\vessel-app\Back-end\vessel_app\__init__.py", line 21, in <module>
    db = SQLAlchemy()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 716, in __init__
    self.Model = self.make_declarative_base(model_class, metadata)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 798, in make_declarative_base
    model.query_class = self.Query
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\sqlalchemy\ext\declarative\api.py", line 79, in __setattr__
    _add_attribute(cls, key, value)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 802, in _add_attribute
    type.__setattr__(cls, key, value)
TypeError: can't apply this __setattr__ to DefaultMeta object

My last guess is that its or its the python version 3.8.4 slim has this error.

File "D:\Openvessel\vessel-app\Back-end\vessel_app\__init__.py", line 21, in <module>
db = SQLAlchemy()

Solution

  • This looks like an issue that was introduced into the python language.

    To fix this:

    You can revert the version of python you are building with, or if you are using docker, fix the python image temporarily using python:3.8.3-slim or an equivalent image. You can also wait until the fix is out.

    You can see the related pull request here: https://github.com/python/cpython/pull/21473

    Introduced here: https://bugs.python.org/issue39960

    Patch bug here: https://bugs.python.org/issue41295

    Another issue in an unrelated package that is the same error: flask_sqlalchemy: error with `__setattr__` to `DefaultMeta`

    Git lab CI Running nose tests with SqlAlchemy. ERROR: Failure: TypeError (can't apply this __setattr__ to DefaultMeta object)

    Edit: A newer version with the fix introduced is out. Check out any image of python:3.8.5 +.