1

Python で作成しているインストーラーで小さな問題が発生しています。キーの位置に基づいてキーの値を返す関数があります。

def CheckRegistryKey(registryConnection, location, softwareName, keyName):
'''
Check the windows registry and return the key value based on location and keyname
'''    
    try:
        if registryConnection == "machine":
            aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
        elif registryConnection == "user":
            aReg = ConnectRegistry(None,HKEY_CURRENT_USER)   
        aKey = OpenKey(aReg, location)
    except Exception, ex:
        print ex
        return False

    try:
        aSubKey=OpenKey(aKey,softwareName)
        val=QueryValueEx(aSubKey, keyName)
        return val
    except EnvironmentError:
        pass

場所が存在しない場合、エラーが発生します。関数が返されるようにしたいFalseので、場所が存在しない場合はソフトウェアインストーラーを実行できますが、ビットは常に例外になります

# check if the machine has .VC++ 2010 Redistributables and install it if needed
try:
    hasRegistryKey = (edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed"))
    if hasRegistryKey != False:
        keyCheck = (edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed"))[0]
        if keyCheck == 1:   
            print 'vc++ 2010 redist installed'
    else:
        print 'installing VC++ 2010 Redistributables'
        os.system(productsExecutables + 'vcredist_x86.exe /q /norestart')
        print 'VC++ 2010 Redistributables installed'
except Exception, ex:
    print ex

コードを実行したときに発生する例外は

'NoneType' object has no attribute '___getitem___'

def CheckRegistryKey関数から得られるエラーは

[Error 2] The system cannot find the file specified

私がする必要があるのは、レジストリ キーまたは場所が存在するかどうかを確認することです。そうでない場合は、実行可能ファイルに直接指定します。どんな助けでも大歓迎です。

ありがとうございました

4

1 に答える 1

2

エラーの理由:

'NoneType' object has no attribute '___getitem___'

行にあります:

keyCheck = (edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed"))[0]

フラグメントedit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed")はを返してNoneいます。

これは、最終的に次のことを意味します。

keyCheck = (None)[0]

これがエラーをスローしているものです。Noneであるオブジェクトのアイテムを取得しようとしています。

Noneこれが関数から返される理由はCheckRegistryKey、エラーが発生した場合、何も返さないためです。return Falseあなたが捕まえるときあなたはする必要がありますEnvironmentError

try:
    aSubKey=OpenKey(aKey,softwareName)
    val=QueryValueEx(aSubKey, keyName)
    return val
except EnvironmentError:
    return False

CheckRegistryKeyまた、 1回だけ呼び出すようにコードを変更します。

registryKey = edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed")
if registryKey is not False:
    keyCheck = registryKey[0]
于 2012-11-08T06:38:12.590 に答える