0

I was recently running a python test program in gitbash. It looks like this:

COST_OF_FRISBEE = 15
num_frisbees = int(input("How many frisbees do you want? "))
result = COST_OF_FRISBEE * num_frisbees
print("The total cost is " + str(result))

and is saved in a file called hw4.py. Unfortunately, when I run the program with the command python3 hw4.py, and type in a number as the program prompts me to, gitbash hangs after I press the enter key on my keyboard. It actually allows me to keep hitting enter multiple times and to continue typing things in. I've screenshotted what this looks like:

gitbash hanging

The screenshot was taken after the following was entered from my keyboard: python3 hw4.py, return, 3, return (3 times), asdf, up arrow key (5 times), Backspace (4 times). You can see from the screenshot that gitbash lets me delete part of the filepath prompt (which should never happen), confusing me greatly. I have also tried ctrl-C'ing and ctrl-D'ing out of it, but nothing happens. The only way to get out of it is to close the whole window out, during which attempt I get a pop-up warning me that there is an ongoing process that I will have to kill. I've tried googling this but the only search results I get are that the command 'python3' isn't recognized on the command line, which is a totally different problem.

Please help! Thanks!

EDIT: I tried the same thing on my computer at home (the original problem occurred on my work laptop) and found something interesting that makes me think that this has to do with my installation of Python, rather than anything to do with buffer flushing. In the following screenshot you can see that when I simply type 'python' and follow the same keystrokes that I described above, I get the same hanging behavior that is so annoying:

python command hanging

This, however, only happens when my .bashrc file looks like this:

export PATH="$PATH:/c/PATH_TO_PYTHON/Python/Python310 : /c/PATH_TO_PYTHON/Python/Python310/Scripts"

When my .bashrc looks like this, instead...

export PATH="$PATH:/c/PATH_TO_PYTHON/Python/Python310 : /c/PATH_TO_PYTHON/Python/Python310/Scripts"
alias python="winpty python.exe"

...I am able to enter the python interpreter, and that works normally, even though it's not what I want. To be clear, I want to be able to run my python file with the command "python3 hw4.py" or "python hw4.py" directly from the gitbash CLI. I've been able to do this in the past, so I know it's possible!

As a last note, if you're trying to reproduce the issue, make sure to close out your gitbash session after you edit the .bashrc file so that your changes from the edit can take effect.

Thanks again for the help.

anonimo
  • 1
  • 3
  • If you start a new terminal and don't use `vim`, then does it work? I'm wondering if `vim` is mucking up the terminal config. You might also try checking your Bash config for anything that changes terminal settings, or try using a plain prompt on the off chance that's causing the problem. – wjandrea Oct 21 '22 at 01:45
  • BTW, welcome to Stack Overflow! Check out [ask] if you want any tips. – wjandrea Oct 21 '22 at 01:45
  • You could also try using another shell like CMD or PowerShell. Or another terminal like Windows terminal. – wjandrea Oct 21 '22 at 01:49
  • Do you *need* to use Git Bash to run Python, for some reason? Or is this just to fix it for its own sake? – Karl Knechtel Oct 21 '22 at 02:00
  • 2
    See Git For Windows [known issues](https://github.com/git-for-windows/build-extra/blob/main/ReleaseNotes.md#known-issues) which states "Some console programs, most notably non-MSYS2 Python, PHP, Node and OpenSSL, interact correctly with MinTTY only when called through `winpty` (e.g. the Python console needs to be started as `winpty python` instead of just `python`)." – Steven Rumbalski Oct 21 '22 at 02:12
  • Thanks for the welcome! See my edit to the OP for my answer to your first question @wjandrea. As for your second suggestion, I need to be able to do this from the gitbash CLI. CMD and PowerShell won't help me. – anonimo Oct 21 '22 at 19:03

1 Answers1

1

Since there is no error in your code, that would point towards a problem with your python install.

A Cross Referenced article to Super User: Git-Bash not running Python3 as expected, hanging issues

Gives advice on using winpty to create an alias that will run python in winpty instead of gitbash.

in gitbash start your python file with winpty python3 <file>.py instead of just python3 <file>.py

AWoods
  • 98
  • 8
  • I made this more clear in my edit to the OP, but I want to be able to run my python file with the command 'python3 h4.py' or 'python hw4.py' directly from the gitbash CLI. I've run python scripts from gitbash before so I know for a fact that it's possible.... – anonimo Oct 21 '22 at 19:06