Search code examples
pythonkeychain

Unlock the key-chain in mac through python


Is there any way to unlock the key-chain in mac through python. I got it in bash as security unlock-keychain -p $KEYCHAIN_PASSWORD $PATH_TO_KEYCHAIN. But couldnt find a solution for the same in python anywhere. Please help me out.


Solution

  • There isn't a Python API for this. Use the subprocess module to call the external program.

    import subprocess
    
    cmd = "security unlock-keychain -p".split() 
    cmd += [keychain_password, path_to_keychain]
    subprocess.call(cmd)
    

    Further reading on subprocess