按照芹菜教程,我得到这个错误 No module named 'src'
Posted
技术标签:
【中文标题】按照芹菜教程,我得到这个错误 No module named \'src\'【英文标题】:following the celery tutorial and I get this error No module named 'src'按照芹菜教程,我得到这个错误 No module named 'src' 【发布时间】:2017-01-21 17:10:40 【问题描述】:我正在关注 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'
【问题讨论】:
请将完整的错误回溯添加到您的问题中。 @KlausD。完成看上面 您的回溯与您的代码不匹配。 【参考方案1】:您的 PYTHONPATH 必须在其中包含 src 目录。如果您执行cd src
,它会将您的模块视为celery
而不是src.celery
。
要修复它,您可以...
A.向上运行你的代码一个目录
cd .. && celery worker -A src.tasks -l info
B.将父目录添加到您的 PYTHONPATH
PYTHONPATH=.. celery worker -A src.tasks -l info
【讨论】:
如果我这样做,它会说没有名为任务的模块 好点 --A
命令是它应该加载的 python 模块,在本例中是 tasks.py
。您可以按照我上面的建议使用src.tasks
修改它,或者您可以将导入语句修改为from celery import app
。这取决于代码中的 import 语句使用 from src import...
构造的次数。以上是关于按照芹菜教程,我得到这个错误 No module named 'src'的主要内容,如果未能解决你的问题,请参考以下文章
Django Channels No module named 'asgiref.sync' 错误
Django:ImportError at / No module named urls
如何解决这个 celery 错误:无法加载 celery 应用程序。模块“预测”没有属性“芹菜”