gpt4 book ai didi

python - 按照 celery 教程,我收到此错误 No module named 'src'

转载 作者:可可西里 更新时间:2023-11-01 11:31:46 24 4
gpt4 key购买 nike

我正在学习 celery 教程,但出现此错误 No module named 'src'。我不明白这是什么问题。

这是我的目录结构

venv/
src/
__init__.py
celery.py
manage.py
tasks.py

我的 celery .py

    from __future__ import absolute_import
from .gettingstarted.settings.local import BROKER_URL
from celery import Celery

app = Celery('src',
broker=BROKER_URL,
backend=BROKER_URL,
include=['src.tasks'])

# Optional configuration, see the application user guide.
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)

if __name__ == '__main__':
app.start()

我的 tasks.py 文件

    from __future__ import absolute_import

from proj.celery import app


@app.task
def add(x, y):
return x + y


@app.task
def mul(x, y):
return x * y


@app.task
def xsum(numbers):
return sum(numbers)

我也试过

from __future__ import absolute_import
import sys
sys.path.insert(0, "/project/src/")
from proj.celery import app

那也没用。

当我进入 src 目录并运行 python shell 时出现以下错误

No module named 'src'

我的故障是什么?

编辑

我的回溯

(practice) apples-MBP:src ray$ celery worker -A tasks -l info
Traceback (most recent call last):
File "/Users/ray/Desktop/myheroku/practice/bin/celery", line 11, in <module>
sys.exit(main())
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/__main__.py", line 30, in main
main()
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/celery.py", line 793, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/base.py", line 309, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/base.py", line 469, in setup_app_from_commandline
self.app = self.find_app(app)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/base.py", line 489, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/app/utils.py", line 235, in find_app
sym = symbol_by_name(app, imp=imp)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/bin/base.py", line 492, in symbol_by_name
return symbol_by_name(name, imp=imp)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/kombu/utils/__init__.py", line 96, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/ray/Desktop/myheroku/practice/src/tasks.py", line 4, in <module>
from src.celery import app
ImportError: No module named 'src'

最佳答案

您的 PYTHONPATH 必须包含 src 目录。如果您执行 cd src,它会将您的模块视为 celery 而不是 src.celery

要修复它,您可以...

一个。在一个目录上运行你的代码

cd .. && celery worker -A src.tasks -l info

B.将父目录添加到您的 PYTHONPATH

PYTHONPATH=.. celery worker -A src.tasks -l info

关于python - 按照 celery 教程,我收到此错误 No module named 'src',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com