[英]Python: run a python script inside the git-bash environment
我在git-bash环境中运行python脚本时遇到问题。 我的应用程序正在从exe容器(py2exe)运行(目前),它应该只在git-bash
环境中执行另一个python脚本。
该应用程序和git-bash.exe位于同一目录内(整个便携式版本git
都提取到该文件夹中)。 我要运行的第二个脚本在名为scripts
的子文件夹中。
这里是将被编译为自可执行文件的python文件:import os
try:
root = os.path.dirname(__file__)
except:
root = os.path.dirname(sys.argv[0])
git = os.path.join(root,"git-bash.exe")
gitScript = os.path.join(root,"scripts","git_deploy.py")
我尝试了不同的变体,但没有成功:
# 1st try:
subprocess.Popen(["python", gitScript], executable=git)
# 2nd try:
subprocess.Popen(["python %s"%gitScript], shell=True, executable=git)
# 3rd try:
subprocess.Popen(["-c", "python", gitScript], executable=git)
# 4th try:
subprocess.Popen([git, "python", gitScript])
# 5th try:
subprocess.Popen([git, "-c", "python", gitScript])
知道我在这里做错了什么吗?
谢谢
我给了以下快速测试,它似乎可以正常工作。 有两件事:
C:\\Program Files (x86)\\Git\\bin
位于系统的PATH
环境变量中 git_deploy.py
的路径将需要用双引号引起来,这意味着您必须使用双反斜杠对这些引号进行转义: \\\\"<path_to_git_deploy.py>\\\\"
示例代码:
import os, shlex, subprocess
from subprocess import Popen, PIPE, STDOUT
gitScript = 'C:\\Users\\MYUSERNAME\\Downloads\\scripts\\git_deploy.py'
command = '"C:\\Program Files (x86)\\Git\\bin\\sh.exe" --login -i -c "python \\"' + gitScript + '\\""'
command_args = shlex.split(command)
process = Popen(command_args, stdout=PIPE, stderr=STDOUT)
output, err = process.communicate()
print output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.