Python getpass.getpass() function call hangs - python-2.7

I am trying to get a prompt that will ask for my password but when I try to call getpass.getpass() it just freezes. I am running on Windows 7 64 bit using Python 2.7 on Canopy.
import sys
import getpass
p = getpass.getpass()
print p

It is correct that Python "effectively freezes because it can't receive the input from standard input", however, for windows you can prefix your command with winpty. Then password can be inputted correctly when started like:
winpty python fileToExecute.py
winpty provides a interface similar to a Unix pty-master in a way that communication is also possible from windows terminals.

Python "effectively freezes because it can't receive the input from standard input". See https://support.enthought.com/entries/22157050-Canopy-Python-prompt-QtConsole-Can-t-run-getpass-or-interactive-OS-shell-commands-or-Windows-process
The fix is to use a different interpreter. I switched to IDLE and fixed the issue.

Faced the same issue with getpass (mingw64) and found this simple solution.
os.system("stty -echo")
password = input('Enter Password:')
os.system("stty echo")
print("")

getpass() will freeze if python is unable to read properly from standard input.
This can happen on e.g. some Windows terminals, such as using git bash.
You can use the sys module to detect if this will happen, to avoid hanging:
import getpass
import sys
# ...
if not sys.stdin.isatty():
# notify user that they have a bad terminal
# perhaps if os.name == 'nt': , prompt them to use winpty?
return
else:
password = getpass.getpass()
# ...

I also had this on mac with both Jupyter Lab and Jupyter Notebook. For me the issue was caused by the variable name.
Naming the variable PG_REMOTEPASSWORD caused a hang but PG_PASSWORD & PG_ABCPass didn't. I don't know why that is an issue, there's nothing in docs about restrictions on what a variable can be called.
My setup up is Anaconda running Python 3.7.7

In pycharm RUN i had similar issue. When i opened files using Terminal "CMD" issue resolved.

Related

Entering a command to an opened Command Prompt window with python script

I have been trying enter couple of commands on a command window with python script. I was able to open the .exe file by checking the other questions.
However, now my code:
import os
os.chdir(r'filepath')
os.system("start /B start filepath\application.exe")
opens the application and it asks for an input file name, then it will ask for output file name. Is there a way to do that. This will be used for ArcGIS so it needs to be done in python 2.7.
I checked all related questions but did not find the answer. I would appreciate it very much if you could help.
Finally! It was easier than I thought but I needed lots of research. This code for python 2.7 which may differ for other versions.
import os
import subprocess
os.chdir(r'filepath')
subprocess.check_call([r"filepath\your_.exe_app", 'your_input'])

How to use the output of a Linux command as a if/else statement in python

Last week I started using python for writing install scripts for my raspberry pi 3.
I installed debian jessie on my pi and within debian I'm using exagear.
When I start the script, I would like to realize that the code could check in which architecture it's running. With the 'arch' command in Debian I can see it's 'armv71' and within exagear it's 'i686'.
I have two different functions in python, one for the 'armv71' architecture and one for the 'i686' architecture. I would activate them by using a if and else statement.
Could anyone help me solve this problem?
I think you can use subprocess module
import subprocess
myoutput = subprocess.check_output(["arch"])
if myoutput=='x86_64\n':
print 'this is x86'
else:
print 'something else...'

How to run executable with input file using Python?

I am running an executable from cmd:
*.exe input.inp
I want to run it using python and tried following:
os.system('"*.exe"')
But don't know how to specify input file as well. Any suggestions?
import os
from subprocess import Popen, PIPE
p = Popen('fortranExecutable', stdin=PIPE) #NOTE: no shell=True here
p.communicate(os.linesep.join(["input 1", "input 2"]))
For more please refer to:
Using Python to run executable and fill in user input
I had to launch cmd window and specify input file location from within Python script. This page was really helpful in getting it done.
I used Popen(['cmd', '/K', 'command']) from above page and replaced '/K' with '/C' in it to run and close the cmd window.
import os
os.system(r'pathToExe.exe inputFileOrWhateverOtherCommand')

python import cx_Oracle error in command window

I'm having trouble running some codes that import cx_Oracle in command line, though the same codes work in console. Is there anything else I will need to set up in order to get this to work via command line please?
Saved just one line of code "import cx_Oracle" as test.py.
Run this line in ide (Spyder), iPython Notebook => no issues
run this by opening a command line window from the same folder the .py file is saved in, and run python test.py and encounter the below:
import cx_Oracle
ImportError: DLL load failed: %1 is not a valid Win32 application.
Not sure if there is anything additional I will need to set up to run cx_Oracle via command line? Have tried all the suggestions on setting PATH, ORACLE_HOME, re-installing but could not get this to work. versions that i'm using are
Python: 2.7
cx_Oracle: cx_Oracle-5.1.3-11g.win-amd64-py2.7
instant client: 12.1.0.0
Windows: 7 Enterprise
I also found this kind of problem.
Look at "not a valid Win32 application" this sentence, so I decide to change cx_Oracle to cx_Oracle-5.1.3-11g.win-32-py2.7. Luckly, it does work.

Python script.py once saved now won't load as before

I have just started looking & learning Python (on Ubuntu 14.04) using the website: http://learnpythonthehardway.org/book/ and http://www.codecademy.com/
Yesterday, I worked for an hour on a few scripts namely 1-4 and named them as ex1.py etc and they executed fine. Today, I have come back to carry on with a few more exercises and tested my first exercises only to find now I get "SyntaxError: invalid syntax" when attempting "python ex1.py" with or without .py I have tried "#!/usr/bin/python" in any script header also. The path/Dir I have been using to test my Python scripts in is simply 'Python' within my Home Dir. I checked the actual file permissions but that appears to make no difference.
I am not too sure if it's an OS setup issue; Python issue; or simply me. Python seems great, so any ideas on what I'm doing wrong? What has changed since yesterday to my python.py script files?
The problem is that you are in the Python interpreter, and not the Linux terminal. This is indicated both by the error messages you saw and by the >>> prompt.
Exit the interpreter by holding down control and pressing the D key. You will then be back in the Linux terminal and running a script will work the way you expect it to.