import subprocess
login = 'username'
password = 'somepassword'
p = subprocess.Popen(('mkpasswd', '-m', 'sha-512', password), stdout=
    subprocess.PIPE)
shadow_password = p.communicate()[0].strip()
if p.returncode != 0:
    print('Error creating hash for ' + login)
r = subprocess.call(('usermod', '-p', shadow_password, login))
if r != 0:
    pass
is there a way to script in Python to change user passwords in Linux? if so, how?