X Tutup
The Wayback Machine - https://web.archive.org/web/20241209035833/https://github.com/python/cpython/issues/98601
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getpass failing with no module named pwd #98601

Open
fbordignon opened this issue Oct 24, 2022 · 1 comment
Open

getpass failing with no module named pwd #98601

fbordignon opened this issue Oct 24, 2022 · 1 comment
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@fbordignon
Copy link

Bug report

When getpass.getuser() fails to fetch the env variables for Windows, it tries to import pwd, this causes an exception "No module named 'pwd'" on line 168

cpython/Lib/getpass.py

Lines 154 to 169 in f3c23fc

def getuser():
"""Get the username from the environment or password database.
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
"""
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user = os.environ.get(name)
if user:
return user
# If this fails, the exception will "explain" why
import pwd
return pwd.getpwuid(os.getuid())[0]

From what I gather, Windows does not have such module, maybe check the platform before importing?

Your environment

  • CPython versions tested on: 3.9.10
  • Operating system and architecture: Windows 10
@fbordignon fbordignon added the type-bug An unexpected behavior, bug, or error label Oct 24, 2022
@eryksun
Copy link
Contributor

eryksun commented Oct 24, 2022

Several months ago I worked on an enhanced implementation of getpass.getuser() in #90789, but no one else and no core developer showed interest. The proposed implementation not only avoids trying to import pwd on Windows, it implements a working alternative for Windows that's based on the logon session of the process token. It's equivalent to the pwd.getpwuid(os.getuid())[0] call on POSIX.

@eryksun eryksun added OS-windows stdlib Python modules in the Lib dir labels Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants
X Tutup