title: Is there a portable way to get the current username in Python?
body: <p>What is a portable way (e.g. for Linux and Windows) to get the current user's username? Something similar to <code>os.getuid()</code> would be nice:</p>
  <pre><code>&gt;&gt;&gt; os.getuid()
  42
  
  &gt;&gt;&gt; os.getusername()
  'slartibartfast'
  </code></pre>
  <p>The <a href="https://docs.python.org/3/library/pwd.html" rel="noreferrer"><code>pwd</code></a> 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).</p>