python练习贴06 为MinGW配置环境变量

运行MinGW 有一个"Automated MinGW Installer", 运行他的时候,会提示我下载一堆东西。

因为我的网络环境比较恶劣(公司下载会比较麻烦),

所以我现在使用的还是C同学当年给我的版本:MinGW3.1.0. 对应的gcc版本是3.2.3。

他只有一个安装文件,一顿下一步全都搞定(事实上他只有14M,我已经把它放到了我的邮箱里面)

 

然而他也存在着一个不方便的地方,就是安装完后需要手动配置环境变量。需要做的配置大体如下:

 

set PATH=%PATH%;D:\DevelopingTools\MinGW\bin
set LIBRARY_PATH=D:\DevelopingTools\MinGW\lib
set C_INCLUDE_PATH=D:\DevelopingTools\MinGW\include
set CPLUS_INCLUDE_PATH=D:\DevelopingTools\MinGW\include\c++\3.2.3;D:\DevelopingTools\MinGW\include\c++\3.2.3\\mingw32;D:\DevelopingTools\MinGW\include\c++\3.2.3\backward;D:\DevelopingTools\MinGW\include
 

 

一次两次还好,每次都这么配置便有点麻烦。于是参照这篇blog: python练习贴03 修改注册表 。

写了段为MinGW配置环境变量的python代码。如下:

 

#! /usr/bin/python

# MinGW Version:  3.2.3

# --- use this script to set environment value as following: ---
# set PATH=%PATH%;D:\DevelopingTools\MinGW\bin
# set LIBRARY_PATH=D:\DevelopingTools\MinGW\lib
# set C_INCLUDE_PATH=D:\DevelopingTools\MinGW\include
# set CPLUS_INCLUDE_PATH=D:\DevelopingTools\MinGW\include\c++\3.2.3;D:\DevelopingTools\MinGW\include\c++\3.2.3\\mingw32;D:\DevelopingTools\MinGW\include\c++\3.2.3\backward;D:\DevelopingTools\MinGW\include


__author__="Wangqiansheng"
__date__ ="$2009-9-8 15:48:34$"

import win32api
import win32con

# -----------  config data  --------------------------------------------------------------
#pathInReg = 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment'
pathInReg = 'SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment'

minGW_Path  = 'D:\\DevelopingTools\\MinGW\\'
#minGW_Path = 'D:\\Program Files\\MinGW\\'

# ----------------------------------------------------------------------------------------


def setSystemEnvironment(keyName,keyValue):
    """set the windows system environment.
    """
    # open the position where system stores system environment.
    global pathInReg
    key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,pathInReg,0,win32con.KEY_ALL_ACCESS)
    win32api.RegSetValueEx(key,keyName,0,win32con.REG_SZ,keyValue)
    win32api.RegCloseKey(key)

def getSystemEnvironment(keyName):
    """get the windows system environment.
    """
    # open the position where system stores system environment.
    global pathInReg
    key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,pathInReg,0,win32con.KEY_ALL_ACCESS)
    keyVal = win32api.RegQueryValueEx(key,keyName)
    result = keyVal[0]
    win32api.RegCloseKey(key)
    return result


if __name__ == "__main__":
    setSystemEnvironment('PATH',getSystemEnvironment('PATH') + ";" + minGW_Path + "bin")
    setSystemEnvironment('LIBRARY_PATH', minGW_Path + "lib")
    setSystemEnvironment('C_INCLUDE_PATH', minGW_Path + "include")
    setSystemEnvironment('CPLUS_INCLUDE_PATH', minGW_Path + 'include\\c++\\3.2.3;' + minGW_Path + 'include\\c++\\3.2.3\\mingw32;' + minGW_Path + 'include\\c++\\3.2.3\\backward;' + minGW_Path + 'include')
    
 

 

 

你可能感兴趣的:(Python)