all Python 3getpassfunction
getpass.getuser()
user
Returns the current user's name: from environment variables, or from the system user database when they are unset.
Syntax
getpass.getuser()
LOGNAME, USER, LNAME and USERNAME are checked in order, so anyone who sets a variable can fake the name — it is fine for a greeting or a default path, never for access checks. Unlike os.getlogin(), it works without a controlling terminal: in cron, Docker and CI. If no name is found, only OSError is raised since 3.13.
Examples
import getpass
print(getpass.getuser()) # → ? имя текущего пользователя
import getpass, os
os.environ['LOGNAME'] = 'alice'
print(getpass.getuser()) # → alice — переменной окружения достаточно
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy