Python 无法导入在_init__u; py.py中定义的变量

Python 无法导入在_init__u; py.py中定义的变量,python,flask,celery,Python,Flask,Celery,我正在尝试按照中的说明进行操作,以便在芹菜任务中执行烧瓶/socketIO操作。我的目录结构有点不同,但我没有任何运气与进口 我的目录结构如下: ├── app │   ├── __init__.py │   ├── __pycache__ │   ├── auth.py │   ├── ctasks.py │   ├── helper.py │   ├── saml.py │   ├── socks.py │   ├── templates │   ├── threads.py │   └──

我正在尝试按照中的说明进行操作,以便在芹菜任务中执行烧瓶/socketIO操作。我的目录结构有点不同,但我没有任何运气与进口

我的目录结构如下:

├── app
│   ├── __init__.py
│   ├── __pycache__
│   ├── auth.py
│   ├── ctasks.py
│   ├── helper.py
│   ├── saml.py
│   ├── socks.py
│   ├── templates
│   ├── threads.py
│   └── views.py
├── app.py
├── config.py
├── requirements.txt
└── saml
    ├── dev
    └── prod
我从
app.py

from app import socketio, app

if __name__ == '__main__':
    socketio.run(app, debug=True, port=443, ssl_context='adhoc')
from flask import Flask, request
from flask_socketio import SocketIO
from .ctasks import subtaskcaller, make_celery
from .helper import wait_to_finish

async_mode = None

app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app, async_mode=async_mode)
cel = make_celery(app)

from .auth import SamlManager
saml_manager = SamlManager()
saml_manager.init_app(app) 
from app import views, socks, saml, helper, ctasks
from celery import Celery
from config import *
from .helper import wait_to_finish, emitter
import time
from app import cel


def make_celery(app):
    c = Celery(app.import_name, backend=CELERY_RESULT_BACKEND, broker=CELERY_BROKER_URL)
    c.conf.update(app.config)
    taskbase = c.Task

    class ContextTask(taskbase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return taskbase.__call__(self, *args, **kwargs)

    c.Task = ContextTask
    return c

@cel.task(name='tasks.tester', serializer='pickle')
def tester():
    emitter('emit from subsubtask')
    for i in range(1, 50):
        time.sleep(1)
        print('test {0}'.format(i))
    x = True
    return x

@cel.task(name='task.subtaskcaller', serializer='pickle')
def subtaskcaller():
    emitter('emit from subtask')
    finished = tester.delay()
    wait_to_finish(finished)
    return finished
\uuuu init\uuuuu.py

from app import socketio, app

if __name__ == '__main__':
    socketio.run(app, debug=True, port=443, ssl_context='adhoc')
from flask import Flask, request
from flask_socketio import SocketIO
from .ctasks import subtaskcaller, make_celery
from .helper import wait_to_finish

async_mode = None

app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app, async_mode=async_mode)
cel = make_celery(app)

from .auth import SamlManager
saml_manager = SamlManager()
saml_manager.init_app(app) 
from app import views, socks, saml, helper, ctasks
from celery import Celery
from config import *
from .helper import wait_to_finish, emitter
import time
from app import cel


def make_celery(app):
    c = Celery(app.import_name, backend=CELERY_RESULT_BACKEND, broker=CELERY_BROKER_URL)
    c.conf.update(app.config)
    taskbase = c.Task

    class ContextTask(taskbase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return taskbase.__call__(self, *args, **kwargs)

    c.Task = ContextTask
    return c

@cel.task(name='tasks.tester', serializer='pickle')
def tester():
    emitter('emit from subsubtask')
    for i in range(1, 50):
        time.sleep(1)
        print('test {0}'.format(i))
    x = True
    return x

@cel.task(name='task.subtaskcaller', serializer='pickle')
def subtaskcaller():
    emitter('emit from subtask')
    finished = tester.delay()
    wait_to_finish(finished)
    return finished
ctasks.py

from app import socketio, app

if __name__ == '__main__':
    socketio.run(app, debug=True, port=443, ssl_context='adhoc')
from flask import Flask, request
from flask_socketio import SocketIO
from .ctasks import subtaskcaller, make_celery
from .helper import wait_to_finish

async_mode = None

app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app, async_mode=async_mode)
cel = make_celery(app)

from .auth import SamlManager
saml_manager = SamlManager()
saml_manager.init_app(app) 
from app import views, socks, saml, helper, ctasks
from celery import Celery
from config import *
from .helper import wait_to_finish, emitter
import time
from app import cel


def make_celery(app):
    c = Celery(app.import_name, backend=CELERY_RESULT_BACKEND, broker=CELERY_BROKER_URL)
    c.conf.update(app.config)
    taskbase = c.Task

    class ContextTask(taskbase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return taskbase.__call__(self, *args, **kwargs)

    c.Task = ContextTask
    return c

@cel.task(name='tasks.tester', serializer='pickle')
def tester():
    emitter('emit from subsubtask')
    for i in range(1, 50):
        time.sleep(1)
        print('test {0}'.format(i))
    x = True
    return x

@cel.task(name='task.subtaskcaller', serializer='pickle')
def subtaskcaller():
    emitter('emit from subtask')
    finished = tester.delay()
    wait_to_finish(finished)
    return finished
ctasks.py
中尝试从应用程序导入cel时出错:


ImportError:无法在
\uuu init\uuuuuuuuuuupy
中导入名称“cel”
,您只有
cel
。您的
\uuu init\uuu
文件中没有名为
芹菜的对象,因此无法将其导入到其他文件中。您可以从app import cel尝试

编辑:

\uuuu init\uuuuu
中,从.ctasks导入子TaskCaller,制作芹菜

但在
ctasks
中,您可以从app导入
cel
(当时还不存在,当时只有Flask、request和SocketIO存在)


因此,您需要将您的
@cel
修饰函数放在另一个脚本中,您可以在
\uuuuu init\uuuuu

的底部导入该脚本。我相信我正确使用了该单元,并且在过去使用过类似的导入。我猜我有一些问题,循环导入或导入依赖性问题。我的原始代码有一个输入错误,已经更新了。