关于Python的几个过坑指南(MySQL-python、tornado)

最近碰到的Python项目中,需要安装MySQL-python依赖,并且项目使用tornado,以下是记录在其中碰到的坑。

0x01 tornado在render渲染html页面时报错

访问页面时服务器响应500,查看错误日志,提示:

'utf8' codec can't decode byte 0xbf in position 15:invalid start byte

疑惑了很久文件编码格式等,后来debug跟踪发现错误,是项目路径出现问题,最好不要在包含中文路径中:

关于Python的几个过坑指南(MySQL-python、tornado)
snipaste20180809_155405.png

0x02 MySQL-python 1.2.3安装

项目需要使用MySQL-python 1.2.3,然而通过pip安装会一直失败:

关于Python的几个过坑指南(MySQL-python、tornado)
snipaste20180809_155857.png

似乎是注册表中找不到某个值

可以通过以下步骤解决:

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()
关于Python的几个过坑指南(MySQL-python、tornado)
snipaste20180809_160020.png

2、下载MySQL-python-1.2.3.win-amd64-py2.7.exe,直接安装:

http://www.codegood.com/archives/129

关于Python的几个过坑指南(MySQL-python、tornado)
snipaste20180809_160456.png

以上步骤必须先进行注册表注册,运行MySQL-python-1.2.3.win-amd64-py2.7.exe才不会报错找不到环境

上一篇:采集 Nginx 日志的几种方式,你知道几种?


下一篇:深入理解Android插件化技术