kombu icon indicating copy to clipboard operation
kombu copied to clipboard

AttributeError: 'NoneType' object has no attribute 'Redis' when using Celery with Redis

Open jamesjohnson1025 opened this issue 1 year ago • 2 comments

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")

jamesjohnson1025 avatar Jul 31 '24 09:07 jamesjohnson1025

pip install redis

swcasimiro avatar Oct 10 '24 22:10 swcasimiro

I have the same issue, but I configured celery with supervisor. It works well in any manual configuration.

SandeepaDevin avatar Nov 20 '24 11:11 SandeepaDevin

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.

aryamandarda avatar Dec 13 '24 00:12 aryamandarda

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

adi-lb-phoenix avatar Mar 19 '25 08:03 adi-lb-phoenix

after installing redis with docker, redis-python binding has to be installed as well. check

pip show celery pip show redis

auvipy avatar Mar 20 '25 14:03 auvipy