views:

52

answers:

2

I'm looking for a cross platform solution for getting current login/username in Python.

I was surprised that os.getlogin() is only supported under Unix and even there is not necessarly returning what you would expect.

+6  A: 

getpass.getuser() is your friend.

Amber
+1 that's a nifty module
aaronasterling
+1  A: 

Here is what I use:

import os
username = getattr(os, "getlogin", None)
if not username:
    for var in ['USER', 'USERNAME','LOGNAME']:
        if  var in os.environ:
            username = os.environ[var]
print("username: %s" % (username))
Sorin Sbarnea
You're basically reinventing the `getpass` wheel.
Amber