if not username:
    for var in ['USER', 'USERNAME', 'LOGNAME']:
        if var in os.environ:
            username = os.environ[var]
print('username: %s' % username)
Cross platform solution for getting current login name in Python