Reputation: 876
I keep getting this error with my code: AttributeError: module 'socketio' has no attribute 'on'
I have uninstalled and reinstalled flask-socketio and it doesn't help. I have also tried python-socketio and python engineio with the same errors. Here is my file structure:
micro/
env/
app/
__init__.py
main/
__init.py__
routes.py
For reference here is where socketio pulls from whether I do "import socketio" or "from flask_socketio import socketio"
<module 'socketio' from 'C:\\Users\\jonat\\facti\\env\\lib\\site-packages\\socketio\\__init__.py'>
This is my routes.py code:
from flask_socketio import emit, join_room, leave_room, socketio
#this is the first on attribute that causes the crash.
@socketio.on('joined', namespace='/chat')
def joined(message):
print('joined')
"""Sent by clients when they enter a room.
A status message is broadcast to all people in the room."""
room = session.get('room')
join_room(room)
emit('status', {'msg': session.get('name') + ' has entered the room.'}, room=room)
Here is the app init.py
from flask_socketio import SocketIO
socketio = SocketIO()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
socketio.init_app(app)
Here is my pip freeze:
(env) PS C:\Users\jonat\facti> pip freeze
alembic==1.7.6
Babel==2.9.1
bidict==0.22.0
blinker==1.4
certifi==2021.10.8
cffi==1.15.0
chardet==4.0.0
charset-normalizer==2.0.11
click==8.0.3
colorama==0.4.4
cryptography==36.0.1
defusedxml==0.7.1
Deprecated==1.2.13
dnspython==2.2.0
docutils==0.18.1
dominate==2.6.0
elasticsearch==7.17.0
email-validator==1.1.3
Flask==2.0.2
Flask-Babel==2.0.0
Flask-Bootstrap==3.3.7.1
Flask-HTTPAuth==4.5.0
Flask-Login==0.5.0
Flask-Mail==0.9.1
Flask-Migrate==3.1.0
Flask-Moment==1.0.2
Flask-SocketIO==5.1.1
Flask-SQLAlchemy==2.5.1
Flask-WTF==1.0.0
gevent==21.12.0
greenlet==1.1.2
guess-language-spirit==0.5.3
gunicorn==20.1.0
httpie==3.0.2
idna==3.3
importlib-metadata==4.10.1
importlib-resources==5.4.0
itsdangerous==2.0.1
Jinja2==3.0.3
lmtpd==6.2.0
lockfile==0.12.2
Mako==1.1.6
MarkupSafe==2.0.1
multidict==6.0.2
numpy==1.22.2
onetimepass==1.0.1
packaging==21.3
Pillow==9.0.1
pycparser==2.21
Pygments==2.11.2
PyJWT==2.3.0
pyOpenSSL==22.0.0
pyparsing==3.0.7
PyQRCode==1.2.1
PySocks==1.7.1
python-daemon==2.3.0
python-dateutil==2.8.2
python-dotenv==0.19.2
python-editor==1.0.4
python-engineio==4.3.1
python-socketio==5.5.2
pytz==2021.3
redis==4.1.2
requests==2.27.1
requests-toolbelt==0.9.1
rq==1.10.1
six==1.16.0
SQLAlchemy==1.4.31
stripe==2.65.0
typing_extensions==4.0.1
urllib3==1.26.8
visitor==0.1.3
Werkzeug==2.0.2
wrapt==1.13.3
WTForms==3.0.1
zipp==3.7.0
zope.event==4.5.0
zope.interface==5.4.0
Upvotes: 0
Views: 2181
Reputation: 876
I solved my problem by importing socketio from app. The code below shows how to initialize socketio and then import it properly with blueprints. Miguel Grinberg explains it here. Thanks for the help everyone!
app init.py
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
import os
from flask import Flask, request, current_app
from flask_socketio import SocketIO
from config import Config
socketio = SocketIO()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
socketio.init_app(app)
events.py
from threading import
from app import db, socketio
from flask_socketio import emit, join_room, leave_room
@socketio.event
def my_event(message):
session['receive_count'] = session.get('receive_count', 0) + 1
emit('my_response',
{'data': message['data'], 'count': session['receive_count']})
File structure for those curious:
micro/
env/
app/
__init__.py
main/
__init.py__
events.py
Upvotes: 1