Created
October 25, 2014 08:22
-
-
Save anjianshi/e46f69efc54d08362912 to your computer and use it in GitHub Desktop.
Help you compile and install mysql-python on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Install mysql-python on Windows using pip was difficult. | |
This script can help you done the task. | |
Requirements: | |
Python 2.7 | |
pip (setuptools >= 6.0, use `pip install -U setuptools` to upgrade) | |
VCForPython27 (http://www.microsoft.com/en-us/download/details.aspx?id=44266 used to compile mysql-python) | |
mysql-connector (http://dev.mysql.com/downloads/connector/c/ choose 32bit(x86) version if your python is 32bit) | |
Thanks to:http://blog.csdn.net/zf_prm/article/details/31360713 | |
""" | |
from subprocess import call | |
import sys | |
import os | |
import zipfile | |
import re | |
import pip | |
import platform | |
def sh(cmd): call(cmd, shell=True) | |
def update_file(path, replacer): | |
with open(path, 'r+') as f: | |
txt = f.read() | |
txt = replacer(txt) | |
f.seek(0) | |
f.write(txt) | |
f.truncate() | |
MYSQL_PYTHON_VER = '1.2.5' | |
MYSQL_CONNECTOR_DIR = os.environ[ | |
'PROGRAMFILES(X86)' if 'PROGRAMFILES(X86)' in os.environ and platform.architecture()[0] == '32bit' else 'PROGRAMFILES' | |
] + r'\MySQL\MySQL Connector C 6.1' | |
TEMP_DIR = os.environ['TEMP'] | |
if __name__ == '__main__': | |
package_name = 'MySQL-python-' + MYSQL_PYTHON_VER | |
os.chdir(TEMP_DIR) | |
pip.main(['install', '-d', './', 'mysql-python==' + MYSQL_PYTHON_VER]) | |
with open(package_name + '.zip', 'rb') as f: | |
zf = zipfile.ZipFile(f) | |
zf.extractall('./') | |
os.chdir(package_name) | |
update_file('site.cfg', lambda txt: re.sub(r'(?<=^connector = ).*$', MYSQL_CONNECTOR_DIR, txt, flags=re.M)) | |
update_file('_mysql.c', lambda txt: txt.replace(r'config-win.h', r'my_config.h')) | |
update_file('setup_windows.py', lambda txt: txt.replace(r'lib\opt', r'lib\vs10')) | |
sh(r'"{}" setup.py install'.format(sys.executable)) | |
os.chdir('../') | |
sh(r'del {}.zip'.format(package_name)) | |
sh('rmdir {} /S /Q'.format(package_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment