在 Git Bash 中启动 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24123128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch Python script in Git Bash
提问by Benjamin
I'm writing git commands through a Python script (on Windows)
When I double click on myScript.py
, commands are launched in the Windows Command Prompt.
I would like to execute them in Git Bash.
我正在通过 Python 脚本(在 Windows 上)编写 git 命令当我双击 时myScript.py
,命令会在 Windows 命令提示符中启动。我想在 Git Bash 中执行它们。
Any idea how to do that without opening Git Bash and write python myScript.py
?
知道如何在不打开 Git Bash 并编写的情况下做到这一点python myScript.py
吗?
回答by scholi
You can create a batch file (.bat) with the following content:
您可以创建一个包含以下内容的批处理文件 (.bat):
"C:\Program Files\Git\git-bash.exe" -c "python myScript.py"
Yu can also make this available for all .py file by editing your regedit as explained here: Adding a context menu item in Windows for a specific file extensionwith the following command:
Yu 还可以通过编辑 regedit 使其可用于所有 .py 文件,如下所述:使用以下命令在 Windows 中为特定文件扩展名添加上下文菜单项:
"C:\Program Files\Git\git-bash.exe" -c "python \"%1\""
回答by GleasonK
in the top of your python file add #!/usr/bin/python
then you can rename mv myScript.py myScript
and run chmod 755 myScript
. This will make it so you can run the file with ./myScript
look into adding the file directory to your path or linking it to the path if you want to be able to run it from anywhere.
在你的 python 文件的顶部添加#!/usr/bin/python
然后你可以重命名mv myScript.py myScript
并运行chmod 755 myScript
. 这将使您可以运行文件并./myScript
查看将文件目录添加到您的路径或将其链接到路径,如果您希望能够从任何地方运行它。