Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 85
    Alternatively (slightly nicer to read): pwd.getpwuid(os.getuid()).pw_name. Commented Jun 21, 2010 at 14:27
  • 8
    What does it do on Windows? Perhaps one could try this, and if it fails, fall back to the crappy 'getpass/env var' methods. Commented Aug 2, 2014 at 16:46
  • 10
    @JonathanHartley: ImportError: No module named pwd Commented May 26, 2017 at 19:34
  • 3
    This method is on unix-like systems much superior to Konstantin Tenzin's answer, because it handles the sudo case correctly. (I am aware the OP didn';t ask for unix-like ssytems.) Commented Sep 10, 2019 at 7:18
  • 6
    Wrong answer -- this is NOT portable to Windows. Commented Aug 2, 2022 at 21:14