What you need to do is import your add-on module into the main module, but you need to do this after creating the socketio variable, because if you do not encounter circular dependency errors.
Example:
from flask import Flask, render_template from flask_socketio import SocketIO, emit from backend.database import db app = Flask(__name__) socketio = SocketIO(app, engineio_logger=True) @socketio.on('connect') def handle_connection(): from backend.electionAdministration import syncElections syncElections() import electionAdministration
Also, you need to consider that your main Python script will not be called socketservice , because Python always calls the top level script __main__ . So, if you run the above script as the main script, the second file should import socketio as follows:
from __main__ import socketio
This is a little annoyance with Python, which gets worse when you want to have a script that you sometimes run as the main script, but in other cases you also want it to be imported by another script. To make the import work in that case, I use the following trick :
try: from __main__ import socketio except ImportError: from socketservice import socketio
source share