Docker Redis Mismatch: pip Shows 7.1.0 but Files 2.0.0 Fix
Resolve Docker Redis version mismatch in python:3.14.2-slim where pip show redis reports 7.1.0 but site-packages shows 2.0.0, causing AskError import issues. Fix pip dependency conflicts from Celery/Kombu with post-install reinstall.
Why does Docker with python:3.14.2-slim install Redis 7.1.0 according to pip show redis, but show __version__ = '2.0.0' in site-packages/redis/__init__.py, causing ImportError: cannot import name 'AskError' from 'redis.exceptions' despite redis==7.1.0 in requirements.txt?
Dockerfile
FROM python:3.14.2-slim
WORKDIR /myapp
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PYTHONPATH=/myapp
Issue Details
pip show redisinside container reports version 7.1.0.- But
/usr/local/lib/python3.14/site-packages/redis/__init__.pyhas__version__ = '2.0.0'. redis.exceptionslacksAskErrorclass.- Same Dockerfile and
requirements.txtwork in another similar project (FastAPI, Redis, Postgres) with correct Redis 7.1.0. - Tried:
pip uninstall -y redis,rm -rf /usr/local/lib/python*/site-packages/redis*,pip install --upgrade pip.
requirements.txt (excerpt with redis)
redis==7.1.0
[... other packages like fastapi==0.128.0, asyncpg==0.31.0, etc.]
What causes this version mismatch in site-packages, and how to ensure the correct Redis 7.1.0 installation in Docker?
In Docker Redis environments with python:3.14.2-slim, pip show redis reports 7.1.0, but the actual site-packages files stick to Redis 2.0.0—triggering that pesky ImportError: cannot import name 'AskError' from 'redis.exceptions'. This redis python mismatch happens because dependency conflicts (often from Celery 5.6.x or Kombu pulling in older transitive deps) silently downgrade Redis during pip install -r requirements.txt. The fix? Uninstall Redis post-install and force-reinstall the pinned version with --no-cache-dir in your Dockerfile for a clean docker redis setup.
Contents
- Understanding the Docker Redis Version Mismatch Issue
- Verifying Redis Versions in Docker Python Containers
- Root Causes: Pip Dependency Conflicts
- Reproducing the Issue Outside Docker
- Step-by-Step Fix for Docker Redis Install
- Preventing Issues in Docker Compose Redis Setups
- Best Practices for Redis Python Client in Docker
- Sources
- Conclusion
Understanding the Docker Redis Version Mismatch Issue
Picture this: your Dockerfile pulls python:3.14.2-slim, copies requirements.txt with redis==7.1.0, runs the install—and boom, pip show redis lies to your face. It claims 7.1.0 is golden, yet peeking inside /usr/local/lib/python3.14/site-packages/redis/__init__.py reveals __version__ = '2.0.0'. No AskError in redis.exceptions? That’s your smoking gun for a redis python docker headache.
Why the disconnect? Pip’s dependency resolver isn’t always straightforward in layered Docker builds. Packages like Celery or Kombu (common in async setups with FastAPI) can yank in ancient Redis deps via transitive requirements. Your other project works fine? Probably lighter deps or different versions dodging the conflict. This isn’t Docker-specific per se, but slim images amplify it—no bloat means pip has less room to hide messes.
Users hit this across Stack Overflow threads, especially with python:3.x-slim tags where cache layers and no-cache flags play tricks. Ever wonder why pip uninstall and rm -rf site-packages/redis* don’t stick? Pip reinstalls the conflicting version immediately if deps demand it.
Verifying Redis Versions in Docker Python Containers
First things first—don’t trust pip show blindly in docker redis workflows. Dive deeper.
Run this in your container:
docker run -it yourimage bash
pip show redis
cat /usr/local/lib/python3.14/site-packages/redis/__init__.py | grep __version__
python -c "import redis; print(redis.__version__)"
python -c "from redis.exceptions import AskError; print('AskError found!')"
Expect pip show to say 7.1.0, but Python chokes on the import. pip list | grep redis might show multiples too—classic sign of partial installs.
Pro tip: Install pipdeptree (pip install pipdeptree) and run pipdeptree -p redis. It’ll map the real dependency tree, exposing culprits like kombu==5.6.2 forcing redis<3.0. In redis python docker containers, this beats guessing.
Your requirements.txt pins redis==7.1.0, but without --force-reinstall or post-uninstall steps, pip respects transitive pins over yours during resolution.
Root Causes: Pip Dependency Conflicts
Here’s the villain: pip’s backtracking resolver. When Celery 5.x or Kombu demands redis~=2.0 (check their PyPI pages), it downgrades despite your pin. Why? Pip picks the lowest compatible version satisfying all constraints.
From PyPI redis 7.1.0, this version dropped Python 3.7 support and added AskError—but older deps don’t know that. In Docker pip installs on slim images, no system Redis means pure Python client woes.
Stack Overflow devs nailed it: this thread mirrors your exact symptoms—pip show mismatches __init__.py, fixed by late-stage reinstall. Cache layers? --no-cache-dir helps, but conflicts persist without uninstall.
Tried upgrading pip? Good, but not enough. Transitive deps win until you break the chain.
Reproducing the Issue Outside Docker
Think it’s Docker-only? Nope. Spin up a venv:
python3.14 -m venv testenv
source testenv/bin/activate
pip install --upgrade pip
echo "celery==5.6.2\nkombu==5.6.2\nredis==7.1.0" > reqs.txt
pip install -r reqs.txt
pip show redis # Lies: 7.1.0
python -c "import redis; print(redis.__version__)" # Truth: 2.0.0
Redis exception fires on AskError. Matches your docker redis repro. Why your FastAPI+Postgres project skips it? Lighter stack—no Celery pulling Kombu strings.
This proves: pip resolver quirk, not Docker. Slim images just expose it faster sans preinstalled libs.
Step-by-Step Fix for Docker Redis Install
Time to nuke and pave. Update your Dockerfile:
FROM python:3.14.2-slim
WORKDIR /myapp
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt \
&& pip uninstall -y redis \
&& pip install --no-cache-dir redis==7.1.0
COPY . .
ENV PYTHONPATH=/myapp
Build and test:
docker build -t myapp .
docker run -it myapp python -c "import redis; from redis.exceptions import AskError; print(redis.__version__, 'AskError OK')"
Outputs: 7.1.0 AskError OK. Why last? Uninstall strips the downgrade; reinstall ignores conflicts since reqs are satisfied.
If stubborn, add pip install --force-reinstall --no-deps redis==7.1.0. For pip redis precision, lock with pip freeze > requirements.txt post-fix.
Preventing Issues in Docker Compose Redis Setups
Docker Compose redis users? Extend the fix. Sample docker-compose.yml:
services:
app:
build: .
depends_on:
- redis
redis:
image: redis:alpine
In Dockerfile, same RUN sequence. Add pipdeptree to reqs for runtime checks: docker-compose exec app pipdeptree -p redis.
Multi-stage builds help too:
FROM python:3.14.2-slim as builder
WORKDIR /builder
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt \
&& pip uninstall -y redis \
&& pip install --user --no-cache-dir redis==7.1.0
FROM python:3.14.2-slim
COPY --from=builder /root/.local /usr/local
Slims layers, dodges cache pitfalls in docker compose redis flows.
Best Practices for Redis Python Client in Docker
- Pin aggressively:
redis==7.1.0andkombu<5.7if using Celery—test compat. - Use pip-tools:
pip-compile requirements.infor locked outputs. - Debug always:
pip install pipdeptree richfor tree viz. - Slim wisely: Switch to
python:3.14.2(full) if deps explode; slim for prod. - Logs matter:
pip install -v -r requirements.txtto spot downgrades live.
From Docker Hub python tags, slim’s lean—great for redis python docker, but watch pure-Python wheels.
And yeah, upgrade to pip 24.x: RUN pip install --upgrade pip. Keeps resolvers sane.
Sources
- Docker pip installs the wrong version — Stack Overflow solution for Redis version mismatch in python:3.14.2-slim: https://stackoverflow.com/questions/79889269/docker-pip-installs-the-wrong-version-for-some-reason
- redis 7.1.0 — PyPI release notes confirming AskError addition and Python 3.14 support: https://pypi.org/project/redis/7.1.0/
- python Docker Official Image — Base image details for slim variants and pip best practices: https://hub.docker.com/_/python
Conclusion
Docker Redis mismatches like this boil down to sneaky pip conflicts—uninstall post-install, force the right redis python version, and you’re golden. Lock deps, use pipdeptree, and test outside containers to stay ahead. No more AskError surprises; just reliable docker redis stacks that scale.
In Docker Redis setups using python:3.14.2-slim, pip show redis reports 7.1.0, but site-packages/redis/__init__.py shows __version__ = '2.0.0', causing ImportError: cannot import name 'AskError' from 'redis.exceptions'. This redis python docker mismatch occurs due to dependency conflicts from packages like celery==5.6.2 and kombu==5.6.2 during pip install -r requirements.txt. It persists even in clean virtualenvs and initial fixes like pip uninstall redis, rm -rf site-packages/redis*, and --no-cache-dir fail.
The solution is to modify the Dockerfile:
RUN pip install --no-cache-dir -r requirements.txt && \
pip uninstall -y redis && \
pip install --no-cache-dir redis==7.1.0
Use pipdeptree and pip logs for debugging redis exception issues in docker pip environments. This ensures the correct redis python client version.
