我想在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
我想要的是在终端中运行file.py文件,而不在命令(py python或python python )中使用python或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,即可使用./运行该文件。
发布于 2021-10-07 00:31:58
我不清楚你到底想要什么,但我有两个选择。只需运行该文件并使该文件成为可执行文件。
如果您只想运行它,那么使用终端命令python file-name
或python3 file-name
(如果您安装了Python2)。
您不能使用以下命令运行python文件:./file-name
但是,如果您想使其成为可执行文件,请继续阅读。
如果你想创建一个可执行文件,pyinstaller会帮你完成这项工作。如果您有pip,那么运行命令pip install pyinstaller
。然后,一旦安装了pyinstaller,打开终端或命令提示符,并使用cd找到python文件的目录。然后,一旦进入终端/命令提示符目录,如果不希望在运行时打开命令提示符/终端/命令行,则在--onefile之后运行命令pyinstaller --onefile file-name
include -w
。
(Pyinstaller适用于所有操作系统)
发布于 2021-10-07 05:36:00
我只需将shebang行中的python3更改为python,即可使用./运行该文件。
https://stackoverflow.com/questions/69468434
复制相似问题