Redis——解决AttributeError: module ‘redis‘ has no attribute ‘Redis‘

需要基于redis用python实现一个功能,安装好redis后,测试所用的目录结构如下:
Redis——解决AttributeError: module ‘redis‘ has no attribute ‘Redis‘_第1张图片

string.py测试文件中代码如下:

import redis

r = redis.Redis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')

print(r.append('foo', ' bar'))
print(r.get('foo'))

运行后出现如下错误:

Traceback (most recent call last):
  File "F:/PythonProjects/web/redis_in_python/example/string.py", line 1, in <module>
    import redis
  File "F:\PythonProjects\web\redis_in_python\venv\lib\site-packages\redis\__init__.py", line 1, in <module>
    from redis.client import Redis, StrictRedis
  File "F:\PythonProjects\web\redis_in_python\venv\lib\site-packages\redis\client.py", line 10, in <module>
    from redis._compat import (basestring, imap, iteritems, iterkeys,
  File "F:\PythonProjects\web\redis_in_python\venv\lib\site-packages\redis\_compat.py", line 155, in <module>
    from string import ascii_letters
  File "F:\PythonProjects\web\redis_in_python\example\string.py", line 3, in <module>
    r = redis.Redis(host='localhost', port=6379, db=0)
AttributeError: module 'redis' has no attribute 'Redis'

首先,查看安装的redis模块中是否有Redis,结果是有的:
Redis——解决AttributeError: module ‘redis‘ has no attribute ‘Redis‘_第2张图片
这个包是在pycharmpro的Project Interpreter中完成导入的,所以代码与导入应该不会有问题。

然后,修改文件名”string.py“为“字符串.py”,结果问题解决:
Redis——解决AttributeError: module ‘redis‘ has no attribute ‘Redis‘_第3张图片
所以原因应该是之前的文件名与redis调用的某处代码或文件名产生冲突了,但具体地点尚不清楚。

你可能感兴趣的:(数据库技术,python,redis)