Python Forum
getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY'
#1
When I call getpass.getpass(), I get this error:

Traceback (most recent call last):
_File "<stdin>", line 1, in <module>
_File "C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\getpass.py", line 48, in unix_getpass
__fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
-________________________________^^^^^^^^^^^^
AttributeError: module 'os' has no attribute 'O_NOCTTY'


(I had to use bold text and white/invisible text (for indentation) because the code option in the question posting menu didn't work)

I am using Python 3.11.1 on Windows 11.
Reply
#2
It's stating that the module os that is imported does not have these attributes - os.O_RDWR os.O_NOCTTY
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Quote:(I had to use bold text and white/invisible text (for indentation) because the code option in the question posting menu didn't work)
Use python tags, not code tags. The code tags don't work.

getpass() works fine on my windows 10 machine running python 3.10.

I wonder why getpass on windows calls unix_getpass? This is the code in getpass.py for python 3.11 that controls what happens when you call getpass. It is different than the same code for 3.10, but it is easy to see what must be happening:
# Bind the name getpass to the appropriate function
try:
    import termios
    # it's possible there is an incompatible termios from the
    # McMillan Installer, make sure we have a UNIX-compatible termios
    termios.tcgetattr, termios.tcsetattr
except (ImportError, AttributeError):
    try:
        import msvcrt
    except ImportError:
        getpass = fallback_getpass
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass
If you have a UNIX-compatible termios, calling getpass will call unix_getpass. Did you install a package named termios that mimics the unix/linux termios? If you did, and if you need the termios module, you'll have to edit the C:\Users\surfer\AppData\Local\Programs\Python\Python311\Lib\getpass.py file to modify the test used to determine if the platform is windows or unix.
try:
    raise ImportError("The termios module I installed brakes this logic.  This is my fix.")
    import termios
    # it's possible there is an incompatible termios from the
    # McMillan Installer, make sure we have a UNIX-compatible termios
    termios.tcgetattr, termios.tcsetattr
except (ImportError, AttributeError):
    try:
        import msvcrt
    except ImportError:
        getpass = fallback_getpass
    else:
        getpass = win_getpass
else:
    getpass = unix_getpass
Reply
#4
I fixed it by simply modifiying getpass.py like this:
Adding "import msvcrt" to the imports in the first lines of the module
And mofying the last code line (an else statement) to "getpass = win_getpass"

"""Utilities to get a password and/or the current user name.

getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
getuser() - Get the user name from the environment or password database.

GetPassWarning - This UserWarning is issued when getpass() cannot prevent
                 echoing of the password contents while reading.

On Windows, the msvcrt module will be used.

"""

# Authors: Piers Lauder (original)
#          Guido van Rossum (Windows support and cleanup)
#          Gregory P. Smith (tty support & GetPassWarning)

import contextlib
import io
import os
import sys
import warnings
import msvcrt # added

# ...

else:
    getpass = win_getpass # unix_getpass
This might have broken Unix support but I don't really care since I use Windows.
Reply
#5
But why was it failing? Windows python installations do not have termios. importing termios should raise an import error. Did you install a package named termios that has attributes tcgetattr and tcsetattr?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,752 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 764 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,919 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,398 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,684 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,143 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,429 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,913 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,648 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  Module 'time' has no attribute 'clock' Sophie 4 3,132 Jan-25-2022, 08:05 PM
Last Post: Sophie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020