0

Before I start just wants to tell you all that i'm a beginner in python :)

I'm running a python script via git bash while committing code changes(using git hook[commit-message]) and I wants to read some user input from git bash terminal and the same has to be sent to my python script where i'll validate that, I tried many options that are available in python modules like getpass, subprocess, msvcrt and others but nothing worked for me. So could someone guide me or show me the code how to achieve this?

Below is the one of the sample code I have used,

import sys
sys.stderr.write("Enter> ")
s=input("")
print(s)

and the output is below, actually the terminal control(stdin) doesn't wait to get user input instead it simply ended. Output of Git Bash terminal

Referred below links but nothing worked out for me :( bash script which executes a python script which prompts user GIT hook -> Python -> Bash: How to read user input?

5
  • 1
    Why are you using sys.stderr anyway? It doesn't seem like an error. Commented Jun 27, 2020 at 16:22
  • You can use bash script to achieving this, if you interested tell me to post an answer Commented Jun 27, 2020 at 16:33
  • 1
    You can get user input byread bash command then pass it to python script. Like: read param then python3 myapp.py $param Commented Jun 27, 2020 at 16:41
  • @Peyman Majidi I'm really interested can you show me the example please, also I wants to make it clear that this python script will be called as part of git hook(commit-message) and the input I wants to read is a password of windows which will be used to authenticate the Jira portal, so ideally the prompt would come from python script so that I can read that value and call the Jira portal using requests module. I would like to have all these logic to be in Python script, is that possible? I appreciate your time on this :) Commented Jun 27, 2020 at 17:12
  • Check out my answer, and fill free for further details Commented Jun 28, 2020 at 14:14

2 Answers 2

3

I suggest you read user-input via bash terminal then pass it as a parameter to your Python script.

This is going to be your Python script file:

import sys

def main():
    if len(sys.argv) == 1:
        print(1) # break commiting proccess 
        return
    username = sys.argv[1]
    password = sys.argv[2]
 
    # stuff to do before commiting (git hook) 

    if username == 'peyman' and password == '2020':
        print(0) # print 0 if everything works fine and you want to continue commiting
    else:
        print(1) # print non-zero if something goes bad and you want to break commiting proccess

if __name__ == "__main__":
    main()

and this is going to be your pre-commit file in the .git/hook/📂 folder:

#!/bin/bash
echo "Username:"
exec < /dev/tty
read Username
echo "Password:"
read Password
x=$(python main.py $Username $Password)
exit $x

As you know, if pre-commit exit with 0, the committing process will execute, and if pre-commit return non-zero, committing will abort. So if you print(0) in your Python script, the output set to exit command like this: exit 0

As you said all the process done in the Python script file

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Peyman Majidi, At last your solution worked perfectly for me. As you said I have moved the bash script to commit-message hook where I read the password and send it to the python script. Only concern I have now is I had to provide absolute path like this x=$(python /C/Users/sb21511/Documents/lmratesmarketdata/.git/hooks/Hello.py $1 $passvar) I tried to give relative path but it did not work, I might do google for this to have relative path later. Once again thanks Peyman Majidi :) Have a nice day!!!
but you can add the python script path to the $PATH Environment variable, then It works, read this answer for more: stackoverflow.com/questions/9546324/…
in MyComputer->Properties->Advanced->Env Variables->Path. (docs.alfresco.com/4.2/tasks/fot-addpath.html)
Sure @Peyman Majidi, let me take a look on this, Thanks once again.
Finally I used this approach SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" to take the python script directory instead of taking it from env $PATH.
2

I don’t think you want stderr. Try this:

s = input("Enter> ")
print(s)

Update: that doesn't seem to work for you. The other way to read input in Python is as follows:

>>> x = sys.stdin.readlines(1)
testing
>>> x
['testing\n']
>>> x[0].rstrip('\n')  # isolate the input
'testing'

However, this is probably a problem with the environment the script is being run in. Could you provide more detail in the question, so that we can reproduce your issue? Otherwise, the answer you find seems to solve your issue https://stackoverflow.com/a/46016718/13676619.

2 Comments

git bash terminal doesnt read the user input when i used input() function
See updated? (By the way, the answer you found does seem to work.) stackoverflow.com/a/46016718/13676619

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.