简体   繁体   中英

From python, run python script in git-bash

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM