2

我在运行 celery 任务时遇到问题,因为它找不到我的模块之一:

(ff)bash-3.2$ flipfinder_app/manage.py celeryd
[...]
Traceback (most recent call last):
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/billiard/process.py", line 248, in _bootstrap
self.run()
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/billiard/process.py", line 97, in run
self._target(*self._args, **self._kwargs)
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/billiard/pool.py", line 268, in worker
initializer(*initargs)
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/concurrency/processes/__init__.py", line 51, in process_initializer
app.loader.init_worker()
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/loaders/base.py", line 115, in init_worker
self.import_default_modules()
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/djcelery/loaders.py", line 136, in import_default_modules
super(DjangoLoader, self).import_default_modules()
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/loaders/base.py", line 110, in import_default_modules
| self.builtin_modules]
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/loaders/base.py", line 96, in import_task_module
return self.import_from_cwd(module)
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/loaders/base.py", line 104, in import_from_cwd
package=package)
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/utils/imports.py", line 96, in import_from_cwd
return imp(module, package=package)
File "/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/celery/loaders/base.py", line 99, in import_module
return importlib.import_module(module, package=package)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/apps/tabs/keywords/tasks.py", line 11, in <module>
from apps.util.adsense import has_adsense
ImportError: No module named adsense

它确实存在:

(ff)bash-3.2$ pwd
/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/apps/util
(ff)bash-3.2$ ls | grep adsense
adsense.py

当我使用 django shell 时,它可以正常导入。

(ff)bash-3.2$ ff_app/manage.py shell
Python 2.7.3 (default, Jan  9 2013, 09:25:40) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.65))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from apps.util.adsense import has_adsense
>>> has_adsense
<function has_adsense at 0x10d3171b8>

我将此添加到任务文件中:

import sys
print sys.path

并在我尝试运行 celery 时看到此输出:

['/Users/jasonlfunk/Workspace/Work/csm/ff-app', '/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/lib',
 '/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/apps', '/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/lib',
 '/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app/apps', '/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
 '/Users/jasonlfunk/.virtualenvs/ff/src/pywhois', '/Users/jasonlfunk/.virtualenvs/ff/src/django-filter',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python27.zip', '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/plat-darwin', '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/plat-mac',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/lib-tk',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/lib-old', '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages', '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/PIL',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/newrelic-1.5.0.103',
 '/Users/jasonlfunk/.virtualenvs/ff/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

您可以看到它/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app位于应该导入的路径中。

我还将此添加到任务文件中:

import os
os.chdir('/Users/jasonlfunk/Workspace/Work/csm/ff-app/ff_app')

一切正常。

 -------------- celery@Jason-Funks-MacBook-Pro.local v3.0.11 (Chiastic Slide)
---- **** ----- 
--- * ***  * -- [Configuration]
-- * - **** --- . broker:      django://guest@localhost:5672//
- ** ---------- . app:         default:0x10ddf5810 (djcelery.loaders.DjangoLoader)
- ** ---------- . concurrency: 2 (processes)
- ** ---------- . events:      OFF (enable -E to monitor this worker)
- ** ---------- 
- *** --- * --- [Queues]
-- ******* ---- . celery:      exchange:celery(direct) binding:celery
--- ***** ----- 

[2013-01-16 17:22:53,248: WARNING/MainProcess] celery@Jason-Funks-MacBook-Pro.local ready.

这是怎么回事?为什么 cd'ing 到一个已经在我的路径中的目录可以解决问题?有任何想法吗?

4

1 回答 1

0

是从任务 def中的 apps.util.adsense 导入 has_adsense吗?

于 2013-01-17T17:49:18.400 回答