gpt4 book ai didi

python - 在Python 3中设置执行位后运行Python程序

转载 作者:行者123 更新时间:2023-12-01 04:01:26 24 4
gpt4 key购买 nike

我正在经历Google's Python instructions它引用:

The commands above are the simplest way to run python programs. If the "execute bit" is set on a .py file, it can be run by name without having to type "python" first. Set the execute bit with the "chmod" command like this:

它继续为我提供在我的终端上运行的代码:

~/google-python-exercises$ chmod +x hello.py
~/google-python-exercises$ ./hello.py ## now can run it as ./hello.py
Hello World

但是,当我输入 ./hello.py 时,我得到:

enter image description here

那么,当这可能是 Python 2 的命令时,这与我使用 Python 3 有关吗?

编辑

我刚刚意识到这只能在 Linux/Mac 上运行。如何在 Windows 上执行此操作?

最佳答案

您需要将文件扩展名与Python正确关联。如果您在系统范围内安装(需要管理员权限),则默认情况下应该会发生这种情况,但如果您使用的是第三方发行版,则无法保证,即使如此也可能需要重新启动。

有关在 Windows 上运行 Python 的更多详细信息,请参阅 Python on Windows docs 。看起来如果你在 Python 安装中省略了启动器,它就不会注册文件扩展名;可能需要正确地重新安装来解决这个问题。

否则,如果您无法使文件扩展名关联正常工作,只需运行:

python hello.py

或者从 3.3 及更高版本开始,您可以使用:

py hello.py

它允许您向 py.exe 提供一个参数,以更改要使用的已安装 Python 版本(如果需要)。

您根本不会在 Windows 上使用 ./hello.py,因为这是错误的目录分隔符(Windows 有时允许正斜杠,但在 DOS 提示符下,这看起来像是尝试启动一个名为 . 的程序,带有开关 /hello.py)。您可以使用普通的 hello.py (Windows 始终搜索当前工作目录)或 .\hello.py 如果您想明确的话。

关于python - 在Python 3中设置执行位后运行Python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com