I know that I can do getpass.getuser()
but that shows the user's name, not their account logon username and I want their account logon username
Asked
Active
Viewed 465 times
1

TheRealTengri
- 1,201
- 1
- 7
- 17
-
Have you looked at [this answer](https://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python)? I feel you want exactly the same. – matevzpoljanc Aug 28 '19 at 07:49
2 Answers
1
Try this one:
For Unix
import os
import pwd
pwd.getpwduid(os.getuid())[0]
For Windows
import os
os.getlogin()

Grzegorz Skibinski
- 12,624
- 2
- 11
- 34
-
For some reason it says ```ModuleNotFoundError: No module named 'pwd'```. Do you know how I can fix it? – TheRealTengri Aug 28 '19 at 12:40
-
Because you're probably using windows. I updated my answer with option for windows too.If you are not on windows - try ```pip install pwd``` But I think pwd should be default module... – Grzegorz Skibinski Aug 28 '19 at 13:19