【发布时间】:2021-11-26 19:03:41
【问题描述】:
我想在 Git bash 中运行一个 python 文件,但是如果不在文件名前添加 py,它就不会执行。 Python已安装,但是当我使用快捷方式执行python文件时,出现错误如下-
user@WinPC MINGW64 /d/git_basic/scripts (main)
$ python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
user@WinPC MINGW64 /d/git_basic/scripts (main)
$ ./all_checks.py
Python was not found; run without arguments to install
from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
请指导我。谢谢
编辑 1
我想要的是在终端中运行 python 文件而不在命令中使用 python 或 py(py file.py 或 python file.py)。我想像这样运行它./file.py
这是 all_checks.py 代码-
#! /usr/bin/env python3
import os
import sys
def check_reboot():
# returns true if pc has a pending reboot
return os.path.exists("/run/reboot-required")
def main():
if check_reboot():
print("pending reboot.")
sys.exit(1)
print("No pending reboot.")
sys.exit(0)
main()
编辑 2
只需在 shebang 行中将 python3 更改为 python,我就可以使用 ./ 运行该文件。
【问题讨论】:
-
你的python文件的第一行是什么?它看起来像
#!python还是类似的? -
使用您当前的设置,
python ./all_checks.py可能适合您