首页 > rq总是No module named XX

rq总是No module named XX

使用python rq作为队列,但是参考他们的docs没有运行成功

文件的组织目录:

+ index.py
+ test_work
| __init__.py
| a.py

index.py 代码如下:

#coding: utf-8
import test_work
from test_work import a
from redis import Redis
from rq import Queue


if __name__=='__main__':
	q = Queue(connection = Redis())
	x=0
	while x<10:
		x+=1
		z=2000200+x
		print x
		q.enqueue(a.count_words_at_url,'http://book.douban.com/subject/%d/'%z)

a.py 代码如下:

#coding: utf-8
import requests

def count_words_at_url(url):
	resp = requests.get(url)
	return len(resp.text.split())

在运行以后总是失败,在rq-dashboard中查看到的错误消息:

 test_work.a.count_words_at_url('http://book.douban.com/subject/2000201/') from default
d8d8c5ad-3ff9-4212-8e98-339fc5cfcc31
Failed 11 minutes ago
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rq-0.3.5-py2.7.egg/rq/worker.py", line 392, in perform_job
    rv = job.perform()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rq-0.3.5-py2.7.egg/rq/job.py", line 340, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rq-0.3.5-py2.7.egg/rq/job.py", line 133, in func
    module = importlib.import_module(module_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named test_work.a

额……python基础没有打牢,求解~


你已经 from test_work import a 了,那就直接用 a.count_words_at_url(...) 即可。

至于 ImportError: No module named test_work.a 的错误,那是因为你的 test_work/__init__.py 里面没有 import a 。所以即便你 import test_work 也不能直接 test_work.a.foo 这么用。

【热门文章】
【热门文章】