ImportError: No module named redis
Asked Answered
R

6

40

I have installed redis using sudo apt-get install redis-server command but I am receiving this error when I run my Python program: ImportError: No module named redis

Any idea what's going wrong or if I should install any other package as well? I am using Ubuntu 13.04 and I have Python 2.7.

Ratter answered 10/10, 2013 at 6:48 Comment(1)
pypi.python.org/pypi/redisChivalric
W
63

To install redis-py, simply:

$ sudo pip install redis

or alternatively (you really should be using pip though):

$ sudo easy_install redis

or from source:

$ sudo python setup.py install

Getting Started

>>> import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'

Details:https://pypi.python.org/pypi/redis

Warden answered 10/10, 2013 at 6:51 Comment(3)
I installed redis through pip, and it works fine with my console. But when I use crontab or jenkins to run it, the error again "ImportError: No module named redis"Varia
You need to specify the path to the python you are using. @harperkooWarden
You shouldn't use sudo with pipFlotage
I
6

I was facing the same issue and this is how I resolved it. Check if you use a virtualenv named dev then don't do

sudo pip install redis 

but just

pip install redis

This will install the redis package in your own virtualenv instead of your "complete" system, and this time your redis package will be found from your code.

Integrant answered 26/10, 2016 at 10:13 Comment(0)
D
1

I had the same issue, the error was that the default pip was 'pip3', and the redis package was installed under python3 packages.

This is not a redis specific issue, but if this is the case for you, try running:

sudo pip2 install redis
Digitalis answered 4/10, 2018 at 7:30 Comment(0)
S
1

I had this issue and nothing helped until I realised that I had created a file "redis.py" so that import redis would import this file instead of the actual library...)

Sumerology answered 16/1, 2023 at 14:17 Comment(0)
P
0

I've noticed other answers are all but stating the obvious of doing a pip install or running the setup.py script from the source. These are clearly helpful for some but there are other potential issues that might be preventing the import redis statement from running without error.

I've been bitten by this situation where my default Python version did not match up with the pip version and install path.

For example:

pip install redis

Will install redis but using the path to Python3.11 and my interpreter (i.e. default path for Python) was using Python3.09.

You will in effect need to make the change from one end or the other. Either by setting your default interpreter to v3.11 or by using the pip install command for the correct default interpreter v3.09 or whichever is in use.

Hope this helps!

Pleiad answered 22/2, 2023 at 17:39 Comment(0)
T
0

I found that my Visual Studio Code was launching a different version than my command window. When I did pip install redis, the command window found python in path differently than Visual Studio Code.

I needed to update my Visual Studio Code to reflect the updated python.

How can I change the Python version in Visual Studio Code?

Tchad answered 6/4, 2023 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.