So, standard python module getpass.py has this function
def getuser(): """Get the username from the environment or password database.
First try various environment variables, then the password database. This works on Windows as long as USERNAME is set.
"""
import os
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): user = os.environ.get(name) if user: return user
# If this fails, the exception will "explain" why import pwd return pwd.getpwuid(os.getuid())[0]
Of course in the case of windows service there is no USERNAME, so that function will fall to import pwd.
So, standard python module getpass.py has this function
def getuser():
"""Get the username from the environment or password database.
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
"""
import os
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): get(name)
user = os.environ.
if user:
return user
# If this fails, the exception will "explain" why os.getuid( ))[0]
import pwd
return pwd.getpwuid(
Of course in the case of windows service there is no USERNAME, so that function will fall to import pwd.