Home > Software design >  running shell command in python under git-bash not working well
running shell command in python under git-bash not working well

Time:12-20

I am using python3 under git-bash environment, and sometimes it does not run shell command well.

#!/usr/bin/env python3


import subprocess as sp

print("hello")

print(sp.getoutput("ls -l"))  # This works.

print(sp.getoutput("date"))   # This hangs and cannot terminate with ctrl-c.

This does not happen when running under normal linux/bash environment.

Then I come across this one: Python not working in the command line of git bash.

I can run using "winpty python ...", however it still cannot terminate even with ctrl-c.

I take back, getoutput("date") hangs but check_output works.

CodePudding user response:

You are needlessly running those commands with a shell.

Prefer this form:

print(sp.check_output(["ls", " -l"]))

print(sp.check_output(["date"]))

  • Related