Sharing an SQLAlchemy DB instance across modules, can't do it with only one Flask instance

Viewed 23

https://github.com/CodeHostedHere/channel_response_bot

I want to create a db instance and share it between my two modules "messages" and "slash_commands". Extensions.py contains the code to create this and is imported in both modules. I would like to import the Flask(app) from app.py but it results in an error.

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from slash_commands import slash_cmds_bp
  File "/opt/channel_response_bot/slash_commands/__init__.py", line 5, in <module>
    from . import views
  File "/opt/channel_response_bot/slash_commands/views.py", line 10, in <module>
    from extensions import db, bolt_app, client, handler
  File "/opt/channel_response_bot/extensions.py", line 12, in <module>
    from app import flask_app
  File "/opt/channel_response_bot/app.py", line 2, in <module>
    from messages import messages_bp
  File "/opt/channel_response_bot/messages/__init__.py", line 5, in <module>
    from . import views
  File "/opt/channel_response_bot/messages/views.py", line 10, in <module>
    from extensions import db, bolt_app, client, handler
ImportError: cannot import name 'db' from 'extensions' (/opt/channel_response_bot/extensions.py)

I also can't do an import from __init__.py in the same level using from . import app as this results in ImportError: attempted relative import with no known parent package

I've been battling blueprints and database issues a lot so my my head is completely fried on this one, I'll need some serious ELI5 to get me across the line

1 Answers

Try using:

from ..extensions import db, bold_app, client, handler

Related