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:
Is in the line:
The fragment
edit_registry.CheckRegistryKey("machine", r"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist", "x86", "Installed")is returningNone.This means you end up with:
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
Noneback from yourCheckRegistryKeyfunction is that if an error occurs you aren’t returning anything. You need toreturn Falsewhen you catch aEnvironmentError:I would also modify your code so that you are only calling
CheckRegistryKeyonce: