stackoverflow

View on StackOverflow

The easiest way to get the current user name is definitely username.

pip install username

then:

import username
    
print(username())
# your_username

And there is also username-cli, a CLI tool to get your username, just:

pip install username-cli

then:

$ username
# -> your username

Works on Linux, macOS, and Windows