문제

I am encountering a small hiccup in the installer I am creating in python. I have a function which returns the value of a key based on it's location.

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

I get an error if the location does not exist. I want the function to return False so if the location does not exist so I can run the software installer, bit it always lands up in the exception

# 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

The exception I get when I run the code is

'NoneType' object has no attribute '___getitem___'

and the error I get from the def CheckRegistryKey function is

[Error 2] The system cannot find the file specified

What I need to do is check if the registry key or location exists, if not direct it to an executable. Any help is appreciated.

Thank you

도움이 되었습니까?

해결책

The reason for the error:

'NoneType' object has no attribute '___getitem___'

Is in the line:

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

The fragment edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed") is returning None.

This means you end up with:

keyCheck = (None)[0]

This is what is throwing your error. You are trying to get an item on a object that is None.

The reason that this you are getting None back from your CheckRegistryKey function is that if an error occurs you aren't returning anything. You need to return False when you catch a EnvironmentError:

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

I would also modify your code so that you are only calling CheckRegistryKey once:

registryKey = edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed")
if registryKey is not False:
    keyCheck = registryKey[0]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top