我正在检查在虚拟环境中使用Redis Queue的情况。目录结构如下:
Scripts-V3 (Virtualenv is at this level)
├── RQ
│ ├── countwords.py
│ └── queue.py
这与documentation中给出的示例相同。
queue.py是:
from redis import Redis
from rq import Queue
from countwords import count_words_at_url
import time
# Tell RQ what Redis connection to use
redis_conn = Redis()
q = Queue(connection=redis_conn) # no args implies the default queue
# Delay execution of count_words_at_url('http://nvie.com')
job = q.enqueue(count_words_at_url, 'http://nvie.com')
print(job.result) # => None
# Now, wait a while, until the worker is finished
time.sleep(2)
print(job.result)
我已检查是否已安装必需的软件包(redis,rq):
> pip3 install rq
Requirement already satisfied: rq in /Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages (1.0)
Requirement already satisfied: redis>=3.0.0 in /Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages (from rq) (3.2.1)
Requirement already satisfied: click>=5.0 in /Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages (from rq) (7.0)
但是,当我运行python3 queue.py
时,它会失败:
Traceback (most recent call last):
File "queue.py", line 1, in <module>
from rq import Queue
File "/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/rq/__init__.py", line 6, in <module>
from .connections import (Connection, get_current_connection, pop_connection,
File "/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/rq/connections.py", line 7, in <module>
from redis import Redis
File "/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/redis/__init__.py", line 1, in <module>
from redis.client import Redis, StrictRedis
File "/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/redis/client.py", line 10, in <module>
from redis._compat import (basestring, imap, iteritems, iterkeys,
File "/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/redis/_compat.py", line 106, in <module>
from queue import Queue
File "/Users/abc/Work/driver-pic-orient/Scripts-V3/RQ/queue.py", line 1, in <module>
from rq import Queue
ImportError: cannot import name 'Redis' from 'redis' (/Users/abc/Work/driver-pic-orient/venv/lib/python3.7/site-packages/rq/__init__.py)
我不明白为什么。怎么了?
答案 0 :(得分:0)
这个问题是交叉发布的,并且已经在rq's Github page上得到了回答:
https://github.com/rq/rq/issues/1091
请在追溯中查看此处
File "/Users/shubhamrathi/Work/driver-pic-orient/venv/lib/python3.7/site-packages/redis/_compat.py", line 106, in <module> from queue import Queue
您的本地目录中有一个名为
queue.py
的文件。它会 引起冲突。我认为将queue.py
重命名为<file_name>.py
工作。
基本上,您自己的queue
阴影Python's own queue
。