python - I can't import from __init__ in Flask? -
i want import socketio init.py file exist inside main app folder main controller exist inside controllers folder , can't , error appears says:
importerror: cannot import name socketio
here app tree:
├── add_permissions.txt ├── db_init.py ├── develop │ ├── config.py │ ├── controllers │ │ ├── abonent.py │ │ ├── events.py │ │ ├── __init__.py │ │ ├── panel.py │ │ └── users.py │ ├── extentions.py │ ├── forms.py │ ├── forms.pyc │ ├── __init__.py │ ├── models.py │ ├── plugins.py │ ├── requests.py │ ├── send_sms.py ├── init.sh ├── manage.py
the content of __init__.py create app:
from flask import flask develop.controllers.abonent import abonent_route develop.controllers.panel import panel_route flask_principal import identity_loaded, userneed, roleneed flask_login import current_user develop.extentions import ( login_manager, toolbar, assets_env, cache, moment, principal ) develop.models import ( db, user ) flask_socketio import socketio socketio = socketio() def create_app(config_object): app = flask(__name__) app.config.from_object(config_object) ######## register database ######## db.init_app(app) # csrf.init_app(app) ######## register extentions ######## login_manager.init_app(app) toolbar.init_app(app) assets_env.init_app(app) moment.init_app(app) principal.init_app(app) socketio.init_app(app) ######## register routes ######## app.register_blueprint(abonent_route) app.register_blueprint(panel_route) # identify users roles @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): # set identity user object identity.user = current_user # add userneed identity if hasattr(current_user, 'id'): identity.provides.add(userneed(current_user.id)) # add each role identity if hasattr(current_user, 'roles'): role in current_user.roles: identity.provides.add(roleneed(role.name)) return app
as can see i've initiated socketio app, no errors tell now.
inside panel.py if imported socketio develop error shows whither used from develop import socketio
or from .. import socketio
.
could body tells me problem?, should ?
because line in __init__.py
from develop.controllers.panel import panel_route
is before
socketio = socketio()
when code in panel.py
from develop import socketio
or from .. import socketio
executed, there is nothing called socketio
in develop
namespace, import fail certainly.
so, should adjust position of code lines in __init__.py
fast fix. not still practice, code shall refactored in future.
Comments
Post a Comment