Reputation: 36234
What is a portable way (e.g. for Linux and Windows) to get the current user's username? Something similar to os.getuid()
would be nice:
>>> os.getuid()
42
# Does not currently exist in Python
>>> os.getusername()
'slartibartfast'
The pwd
module works for Unix only. Some people suggest that getting the username under Windows can be complicated in certain circumstances (e.g., running as a Windows service).
Upvotes: 789
Views: 613522
Reputation: 1267
Using only standard python libs:
from os import environ
from sys import platform
def getUser():
return environ["USERNAME"] if platform.startswith("win") else environ["USER"]
user = getUser()
Works on Windows, Mac or Linux.
Upvotes: 9
Reputation: 105
this works for me:
import os
os.cmd("whoami")
print (os.cmd("whoami"))
Upvotes: -4
Reputation: 115011
Windows:
os.environ.get('USERNAME')
Linux:
os.environ.get('USER')
Both:
os.environ.get('USER', os.environ.get('USERNAME'))
Note that environment variables can be modified by the user, so this is a potential security vulnerability. With this method, an attacker can easily fake their username.
Upvotes: 116
Reputation: 301
If you are needing this to get user's home dir, below could be considered as portable (win32 and linux at least), part of a standard library.
>>> os.path.expanduser('~')
'C:\\Documents and Settings\\johnsmith'
Also you could parse such string to get only last path component (ie. user name).
See: os.path.expanduser
Upvotes: 26
Reputation: 2011
Combine os.getuid()
with pwd.getpwuid()
:
import os
import pwd
def get_username():
return pwd.getpwuid(os.getuid())[0]
See pwd
docs for more details.
Upvotes: 177
Reputation: 12948
Look at getpass
module
import getpass
getpass.getuser()
'kostya'
Availability: Unix, Windows
p.s. Per comment below "this function looks at the values of various environment variables to determine the user name. Therefore, this function should not be relied on for access control purposes (or possibly any other purpose, since it allows any user to impersonate any other)."
Upvotes: 1106
Reputation: 506
None of the above worked in my case (scroll down to the actual solution).
The problem I'm getting with all solutions is the wrong username when running commands with sudo
:
psutil
soulution:$ python3
>>> import psutil
>>> psutil.Process().username()
'ubuntu' # OK!
$ sudo su
$ python3
>>> import psutil
>>> psutil.Process().username()
'root' # OK!
$ sudo python3
>>> import psutil
>>> psutil.Process().username()
'root' # WRONG, should be ubuntu!
getpass
solution:$ python3
>>> import getpass
>>> getpass.getuser()
'ubuntu' # OK!
$ sudo su
$ python3
>>> import getpass
>>> getpass.getuser()
'root' # OK!
$ sudo python3
>>> import getpass
>>> getpass.getuser()
'root' # WRONG, should be ubuntu!
pwd
+ os.getuid
solution:$ python3
>>> import os, pwd
>>> pwd.getpwuid( os.getuid() )[ 0 ]
'ubuntu' # OK!
$ sudo su
$ python3
>>> import os, pwd
>>> pwd.getpwuid( os.getuid() )[ 0 ]
'root' # OK!
$ sudo python3
>>> import getpass
>>> getpass.getuser()
'root' # WRONG, should be ubuntu!
os.getlogin
works a bit different, but still wrong:$ python3
>>> import os
>>> os.getlogin()
'ubuntu' # OK!
$ sudo su
$ python3
>>> import os
>>> os.getlogin()
'ubuntu' # WRONG, should be root!
$ sudo python3
>>> import os
>>> os.getlogin()
'ubuntu' # OK!
os.getenv
gives the same results:$ python3
>>> import os
>>> os.getenv('SUDO_USER', os.getenv('USER'))
'ubuntu' # OK!
$ sudo su
$ python3
>>> import os
>>> os.getenv('SUDO_USER', os.getenv('USER'))
'ubuntu' # WRONG, should be root!
$ sudo python3
>>> import os
>>> os.getenv('SUDO_USER', os.getenv('USER'))
'ubuntu' # OK!
Switching SUDO_USER
and USER
gives the wrong result in sudo python3
case.
Solution is a bit tricky and rely on the default root home directory location but works for all cases:
$ python3
>>> import os
>>> 'root' if os.path.expanduser('~') == '/root' else os.getenv('SUDO_USER', os.getenv('USER'))
'ubuntu' # OK!
$ sudo su
$ python3
>>> import os
>>> 'root' if os.path.expanduser('~') == '/root' else os.getenv('SUDO_USER', os.getenv('USER'))
'root' # OK!
$ sudo python3
>>> import os
>>> 'root' if os.path.expanduser('~') == '/root' else os.getenv('SUDO_USER', os.getenv('USER'))
'ubuntu' # OK!
Upvotes: 8
Reputation: 12945
psutil provides a portable way that doesn't use environment variables like the getpass
solution. It is less prone to security issues, and should probably be the accepted answer as of today.
import psutil
def get_username():
return psutil.Process().username()
Under the hood, this combines the getpwuid
based method for unix and the GetTokenInformation
method for Windows.
Upvotes: 20
Reputation: 4020
For UNIX, at least, this works...
import commands
username = commands.getoutput("echo $(whoami)")
print username
edit: I just looked it up and this works on Windows and UNIX:
import commands
username = commands.getoutput("whoami")
On UNIX it returns your username, but on Windows, it returns your user's group, slash, your username.
--
I.E.
UNIX returns: "username"
Windows returns: "domain/username"
--
It's interesting, but probably not ideal unless you are doing something in the terminal anyway... in which case you would probably be using os.system
to begin with. For example, a while ago I needed to add my user to a group, so I did (this is in Linux, mind you)
import os
os.system("sudo usermod -aG \"group_name\" $(whoami)")
print "You have been added to \"group_name\"! Please log out for this to take effect"
I feel like that is easier to read and you don't have to import pwd or getpass.
I also feel like having "domain/user" could be helpful in certain applications in Windows.
Upvotes: 8
Reputation: 348
These might work. I don't know how they behave when running as a service. They aren't portable, but that's what os.name
and if
statements are for.
win32api.GetUserName()
win32api.GetUserNameEx(...)
See: http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html
Upvotes: 26
Reputation:
To me using os
module looks the best for portability: Works best on both Linux and Windows.
import os
# Gives user's home directory
userhome = os.path.expanduser('~')
print "User's home Dir: " + userhome
# Gives username by splitting path based on OS
print "username: " + os.path.split(userhome)[-1]
Output:
Windows:
User's home Dir: C:\Users\myuser
username: myuser
Linux:
User's home Dir: /root
username: root
No need of installing any modules or extensions.
Upvotes: 17
Reputation: 15237
You can get the current username on Windows by going through the Windows API, although it's a bit cumbersome to invoke via the ctypes FFI (GetCurrentProcess → OpenProcessToken → GetTokenInformation → LookupAccountSid).
I wrote a small module that can do this straight from Python, getuser.py. Usage:
import getuser
print(getuser.lookup_username())
It works on both Windows and *nix (the latter uses the pwd
module as described in the other answers).
Upvotes: 3
Reputation: 20569
Combined pwd
and getpass
approach, based on other answers:
try:
import pwd
except ImportError:
import getpass
pwd = None
def current_user():
if pwd:
return pwd.getpwuid(os.geteuid()).pw_name
else:
return getpass.getuser()
Upvotes: 16
Reputation: 71
I wrote the plx module some time ago to get the user name in a portable way on Unix and Windows (among other things): http://www.decalage.info/en/python/plx
Usage:
import plx
username = plx.get_username()
(it requires win32 extensions on Windows)
Upvotes: 3