windows下使用python操作redis(Visual Studio Code)
1.编辑工具: Visual Studio Code(windows环境)
2.redis服务器:这里用了远程连接,需要配置redis.conf。
(1)注释 #bind 127.0.0.1
(2)设置 daemonize no
(3) 设置 protected-mode no (ps:在linux下可用vi进行编辑)
(4)重新启动redis:redis-server redis.conf
3.安装python:在官网下载python安装包,选择全部组件进行安装(这里安装了python2.7),可见python的默认安装路径为C:\Python27\
https://www.python.org/downloads/
4.安装pip。下载地址是:https://pypi.python.org/pypi/pip#downloads :
下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入:python setup.py install
5.安装redis python客户端
在CMD控制台输入:pip install redis
6.新建一个文件夹,用Visual Studio Code打开文件夹,添加一个文件 test.py
7.左侧栏点击扩展,输入python搜索扩展,并安装python
8.左侧栏点击调试菜单,点击设置按钮,并选择python作为编译环境,可看到生成了一个launch.json文件
9.设置lauch.json:
pythonPath:选择python.exe的路径
program:默认启动文件
python.autoComplete.extraPaths:python扩展模块,这里添加了redis模块的路径
"configurations": [ { "name": "Python", "type": "python", "request": "launch", "stopOnEntry": true, "pythonPath": "c:/python27/python.exe", "program": "${workspaceRoot}/test.py", "cwd": "${workspaceRoot}", "debugOptions": [ "WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput" ], "python.autoComplete.extraPaths":[ "C:/Python27/Lib/site-packages/redis" ] }
10.test.py的代码。redis的ip地址为192.168.16.130,端口号为6379,运行后,可看到控制台输出 hello
import redis r = redis.Redis(host='192.168.16.130',port=6379,db=0) r.set('hello','world') print(r.get('hello'))
附:Visual Studio Codo Python扩展官方文档:https://github.com/DonJayamanne/pythonVSCode/wiki