AttributeError: 'NoneType' object has no attribute 'Redis' when using Celery with Redis
I'm encountering an error when trying to use Celery with Redis in my Python project. Despite having installed both Celery and Redis, I'm getting the following error:
class PrefixedStrictRedis(GlobalKeyPrefixMixin, redis.Redis):
AttributeError: 'NoneType' object has no attribute 'Redis'
This error occurs in the Kombu library, which is a dependency of Celery. I've already tried the following steps:
Installed Redis: pip install redis Installed Celery: pip install celery
Verified that the Redis server is running I checked that I am using the correct virtual environment
My environment:
Python version: 3.10
Operating System: macOS
Redis: 4.5.2
Celery: 5.4.0
I am using Redis docker. I tried testing using the below script and it returned true:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
try:
r.ping()
print("Successfully connected to Redis")
except redis.ConnectionError:
print("Failed to connect to Redis")
What could be causing this error, and how can I resolve it? Is there a potential version conflict or misconfiguration that I'm overlooking?
The error is triggered when I try to execute the following train function using my FastAPI endpoint:
# api.py
api.post("/train")
def train():
hello.delay()
# tasks.py
celery_app = Celery('tasks', broker='redis://localhost:6379/0')
celery_app.conf.update(result_backend='redis://localhost:6379/0')
@celery_app.task
def hello():
print("Hello world")
pip install redis
I have the same issue, but I configured celery with supervisor. It works well in any manual configuration.
I am having the same issue and re-installing redis did not work for me. I am trying to containerize my celery app using Docker and I passed many of the same tests that were conducted by James Johnson. Any advice would be appreciated.
pip install redis
@swcasimiro why do I need to pip install redis when I have a redis as docker run -d -p 6379:6379 redis
after installing redis with docker, redis-python binding has to be installed as well. check
pip show celery pip show redis