Discussion:
Implementing C++'s getch() in Python
(too old to reply)
b***@gmail.com
2019-05-25 11:05:58 UTC
Permalink
I'm working on Python 3.7 under Windows. I need a way to input characters without echoing them on screen, something that getch() did effectively in C++. I read about the unicurses, ncurses and curses modules, which I was not able to install using pip.

Is there any way of getting this done?
Shakti Kumar
2019-05-25 11:27:15 UTC
Permalink
Post by b***@gmail.com
I'm working on Python 3.7 under Windows. I need a way to input characters
without echoing them on screen, something that getch() did effectively in
C++.
try getpass module.
Typically this would be,

import getpass
variable = getpass.getpass('your prompt')
Post by b***@gmail.com
https://mail.python.org/mailman/listinfo/python-list
--
Sent from Shakti’s iPhone
b***@gmail.com
2019-05-25 14:00:43 UTC
Permalink
Hi Shakti!

Thanks for your response. I have tried getpass() but got the following warning:

Warning (from warnings module):
File "C:\Users\Binoy\AppData\Local\Programs\Python\Python37-32\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.

And true enough, the input string is echoed. I saw a video where getpass() worked on Linux. So, probably, its a Windows thing.

Still looking for a solution to the same on Windows.
Random832
2019-05-25 19:37:16 UTC
Permalink
Post by b***@gmail.com
Hi Shakti!
File
"C:\Users\Binoy\AppData\Local\Programs\Python\Python37-32\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
And true enough, the input string is echoed. I saw a video where
getpass() worked on Linux. So, probably, its a Windows thing.
getpass works fine on the windows console. Are you running the script in an IDE such as IDLE, PyCharm, etc?
b***@gmail.com
2019-05-26 14:47:06 UTC
Permalink
I've run getpass() on IDLE, Spyder, PyCharm and Mu. All with negative results.
Shakti Kumar
2019-05-26 15:01:44 UTC
Permalink
Post by b***@gmail.com
I've run getpass() on IDLE, Spyder, PyCharm and Mu. All with negative results.
As Random832 pointed out, these IDEs cannot handle the stdout/stdin with getpass
You should use a normal terminal (command prompt) on your windows for
running this program.

You can read more here, https://docs.python.org/3.1/library/getpass.html
Paul Moore
2019-05-25 14:14:46 UTC
Permalink
Post by b***@gmail.com
I'm working on Python 3.7 under Windows. I need a way to input characters without echoing them on screen, something that getch() did effectively in C++. I read about the unicurses, ncurses and curses modules, which I was not able to install using pip.
Is there any way of getting this done?
On Windows, the msvcrt module exposes getch:
https://docs.python.org/3.7/library/msvcrt.html#msvcrt.getch
eryk sun
2019-05-26 15:17:44 UTC
Permalink
Post by Paul Moore
https://docs.python.org/3.7/library/msvcrt.html#msvcrt.getch
I suggest using msvcrt.getwch instead of msvcrt.getch. Both functions
are limited to the basic multilingual plane (BMP, i.e. U+0000 --
U+FFFF), but getch is additionally limited to the console input
codepage. In Windows, getpass.getpass is based on msvcrt.getwch and
msvcrt.putwch.

If you use getch and need the full BMP range, you can temporarily
change the console input codepage to UTF-8 (65001). It's a multibyte
encoding (i.e. 1-3 bytes per BMP code), so the initial getch call has
to be followed by a loop that calls it again while the sequence can't
be decoded and kbhit() is true.

Loading...