From python, run python script in git-bash

Viewed 46

From python, I need to run a python file inside of git bash, while running in Windows.

That is, I have a configuration script written in python that calls other python scripts. Unfortunately, some of them use Unix commands, so they must be run using git bash in Windows.

Currently I'm using this:

cmd = f'{sys.executable} mydependency.py'
pipe = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# waiting for pipe is handled later...

However, this doesn't work, giving me a cannot execute binary file message. How can I get it to run?

PS: For slightly more context, mydependency.py is actually the amalgamate.py script from the simdjson (https://github.com/simdjson/simdjson) project.

EDIT:

I have also attempted the following:

  • Switch to run or call instead of subprocess.Popen
  • Use f'{git_bash_path} {sys.executable} mydependency.py'
  • Change the shell and executable parameters of Popen,run and call
1 Answers

I found a solution:

cmd = git_bash_path # Found with glob.
pipe = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
pipe.communicate(input=f'{sys.executable} mydependency.py'.encode())

I'm not entirely sure why this works, if anyone has an explanation I'd be glad to hear it.

Related