【问题标题】:Python was not found when executing a .py file on Git Bash MINGW64在 Git Bash MINGW64 上执行 .py 文件时找不到 Python
【发布时间】: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 可能适合您

标签: python git-bash mingw-w64


【解决方案1】:

我不清楚您到底想要什么,但我有两个选择。 只需运行文件并使文件成为可执行文件。

如果您只想运行它,请使用终端命令,python file-namepython3 file-name(如果您有任何 python 2 安装)。

您不要使用此命令运行 python 文件:./file-name

但是,如果你想让它成为一个可执行文件,请继续阅读。

如果您尝试制作可执行文件,pyinstaller 将为您完成这项工作。如果您有 pip,则运行命令 pip install pyinstaller。然后,一旦你安装了 pyinstaller,打开终端或命令提示符并使用 cd 找到 python 文件的目录。然后,一旦您在终端/命令提示符的目录中运行命令 pyinstaller --onefile file-name include -w 在 --onefile 如果您不希望命令提示符/终端/命令行在运行时打开。

(Pyinstaller 适用于所有操作系统)

【讨论】:

    【解决方案2】:

    只需在 shebang 行中将 python3 更改为 python,我就可以使用 ./ 运行该文件。

    Python 3 Docs - Shebang lines

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多