Python 导入错误:没有名为 redis 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19288900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:21:16  来源:igfitidea点击:

ImportError: No module named redis

pythonubunturedisinstallpackage

提问by Mona Jalal

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

我已使用sudo apt-get install redis-server命令安装了 redis,但在运行 Python 程序时收到此错误: 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.

知道出了什么问题,或者我是否应该安装任何其他软件包?我使用的是 Ubuntu 13.04,我有 Python 2.7。

采纳答案by sinceq

To install redis-py, simply:

要安装 redis-py,只需:

$ sudo pip install redis

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

或者(不过,您确实应该使用 pip):

$ 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

详情:https: //pypi.python.org/pypi/redis

回答by Suneel Kumar

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

我遇到了同样的问题,这就是我解决它的方法。检查您是否使用名为 dev 的 virtualenv 然后不要这样做

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.

这将在您自己的 virtualenv 中安装 redis 包,而不是在您的“完整”系统中,这一次将从您的代码中找到您的 redis 包。

回答by Roei Bahumi

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

我有同样的问题,错误是默认pip是'pip3',并且redis包安装在python3包下。

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

这不是 redis 特定问题,但如果您是这种情况,请尝试运行:

sudo pip2 install redis