最近碰到的Python项目中,需要安装MySQL-python依赖,并且项目使用tornado,以下是记录在其中碰到的坑。
0x01 tornado在render渲染html页面时报错
访问页面时服务器响应500,查看错误日志,提示:
'utf8' codec can't decode byte 0xbf in position 15:invalid start byte
疑惑了很久文件编码格式等,后来debug跟踪发现错误,是项目路径出现问题,最好不要在包含中文路径中:
0x02 MySQL-python 1.2.3安装
项目需要使用MySQL-python 1.2.3,然而通过pip安装会一直失败:
似乎是注册表中找不到某个值
可以通过以下步骤解决:
1、使用要安装的python环境运行此脚本:register.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
RegisterPy()
2、下载MySQL-python-1.2.3.win-amd64-py2.7.exe
,直接安装:
http://www.codegood.com/archives/129