python3.4學習筆記(二十四) Python pycharm window安裝redis MySQL-python相關方法
window安裝redis,下載Redis的壓縮包
https://github.com/dmajkic/redis/downloads
如redis-2.4.5-win32-win64.zip
下載完后將其解壓放在自己要放的目錄下
如果你是32位的話就進32bit的文件夾,64位就進64bit文件夾
文件夾進去后會看到幾個文件:
redis-benchmark.exe: 性能測試 模擬N個客戶端發送set,get請求
redis-check-aof.exe:更新日志檢查
redis-check-dump.exe:本地數據庫檢查
redis-server.exe:服務程序
打開dos命令欄,進入到redis的目錄下面(如D:\redis),輸入以下命令:
C:\Users\Administrator>d:
D:\>cd redis
D:\redis>redis-server.exe redis.conf
[5184] 22 Jul 15:53:53 * Server started, Redis version 2.4.5
[5184] 22 Jul 15:53:53 * DB loaded from disk: 0 seconds
[5184] 22 Jul 15:53:53 * The server is now ready to accept connections on port 6
379
啟動成功(注:不需要安裝,關閉命令欄就相當於關閉redis服務了,下次啟動需要重新執行上面命令)
然后再打開一個dos命令欄,進入到redis的目錄下,輸入以下命令:
D:\redis>redis-cli.exe -h 127.0.0.1 -p 6379
進入到redis環境,測試一下set get命令:
redis 127.0.0.1:6379> set zdz helloredis
OK
redis 127.0.0.1:6379> get zdz
"helloredis"
redis 127.0.0.1:6379> set arr "{'aa':1122,'bb':444}"
OK
redis 127.0.0.1:6379> keys *
1) "zdz"
2) "arr"
--------------------------------
set key value 存值
get key 取值
=========================================
#redis_class.py
import redis,json
class Credis(object):
def __init__(self):
self.pool = redis.ConnectionPool(host='localhost', port=6379, db=0, socket_timeout=4)
def setRedis(self,key, err, status, msg):
result = redis.Redis(connection_pool=self.pool)
redisValue = {'err':err,'status':status,'msg':msg}
redisValue = json.dumps(redisValue)
return result.set(key,redisValue)
def getRedis(self,key):
result = redis.Redis(connection_pool=self.pool)
return result.get(key)
credis = Credis()
使用例子:
from redis_class import credis
credis.setRedis('key',0,1,'set')
=========================================
Parser安裝
Parser可以控制如何解析redis響應的內容。redis-py包含兩個Parser類,PythonParser和HiredisParser。默認,如果已經安裝了hiredis模塊,redis-py會使用HiredisParser,否則會使用PythonParser。
HiredisParser是C編寫的,由redis核心團隊維護,性能要比PythonParser提高10倍以上,所以推薦使用。
使用easy_install命令安裝:easy_install hiredis
=========================================
pycharm 增加 redis 支持
用pip安裝redis比較順利
D:\Python27>pip install redis
讓pycharm ide上支持import redis不報錯的方法:
在pycharm里 File -> Settings -> Python interpreter
進入到Python interpreter 點擊+按鈕 搜索redis 選擇一個安裝就可以了。
使用python+redis實現實時聊天室--pythoner.org
http://pythoner.org/wiki/701/
===========================================
安裝MySQL-python報錯比較多,但不關mysql的事
Windows下安裝MySQLdb遇到的問題及解決方法
>pip install MySQL-python
error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat).
Get it from http://aka.ms/vcpython27
----------------------------------------
按上面提示到http://aka.ms/vcpython27下載安裝后執行VCForPython27.msi后仍然報下面的錯誤:
_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h':
No such file or directory
error: command 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\
Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit
status 2
---------------------------------------
解決辦法:搜索或者到下面網址下載安裝:MySQL-python-1.2.3.win-amd64-py2.7.exe
MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions | codegood
http://www.codegood.com/archives/129
安裝后再用pip命令提示已經安裝了
C:\Users\Administrator>pip install MySQL-python
Requirement already satisfied (use --upgrade to upgrade): MySQL-python in d:\pyt
hon27\lib\site-packages
在pycharm里 File -> Settings -> Python interpreter 里面添加MySQL-python就可以了
=======================================
下面是設置的截圖:
使用實例代碼請看本人另外一篇博客:
python3.4學習筆記(二十五) Python 調用mysql redis實例代碼 - 流風,飄然的風 - 博客園
http://www.cnblogs.com/zdz8207/p/python_learn_note_25.html