ImportError: No module named redis

Question:

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.

Asked By: Mona Jalal

||

Answers:

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

Answered By: sinceq

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.

Answered By: Suneel Kumar

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
Answered By: Roei Bahumi

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

Answered By: Oleg Yablokov

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!

Answered By: tijko

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?

Answered By: bryoncline