Python Not Working in the Command Line of Git Bash

Git Bash won't run my python files?

Adapting the PATH should work. Just tried on my Git bash:

$ python --version
sh.exe": python: command not found

$ PATH=$PATH:/c/Python27/

$ python --version
Python 2.7.6

In particular, only provide the directory; don't specify the .exe on the PATH ; and use slashes.

How to use python on gitbash

Getting python run in git bash you simply can type winpty python or you can use python -i

Note: if you use winpty python you can exit python with CTRL + Z or exit(), but if you used python -i, you only can exit with exit().

Why does asyncio work in cmd but not in Git Bash?

Per Mofi's comment, I changed my code to this:

import asyncio

async def main():
print('hello', flush=True)
await asyncio.sleep(3)
print('world', flush=True)

asyncio.run(main())

This forces the buffer to flush after both prints, sending it to the terminal. It works in Git Bash now.

Details:
From what I now understand, under the hood, python sets the buffering depending on the detected device. From https://docs.python.org/2/library/functions.html#open, files are "usually line buffered for tty devices and fully buffered for other files". Print uses the default sys.stdout file object to write to.

Running a new module with the following code:
import sys

print(f'{sys.stdout.isatty()=}')

It prints 'sys.stdout.isatty()=True' in cmd but 'sys.stdout.isatty()=False' in git bash, which explains why stdout chooses to fully buffer in git bash, not print until the code exits, and line buffers in cmd.



Related Topics



Leave a reply



Submit