在 Windows 中使用 #!/usr/bin/env python3 shebang [英] using #!/usr/bin/env python3 shebang with Windows

查看:59
本文介绍了在 Windows 中使用 #!/usr/bin/env python3 shebang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行运行 Python 脚本作为 Windows 上的命令——所以没有使用Python"或.py".如果我的脚本被命名为testing.py",我试图将这个名称变成一个命令并从命令行调用testing".

I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.

浏览文档似乎我需要使用这个shebang #!/usr/bin/env python 只要我的路径中有 Python.

Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.

https://docs.python.org/3/using/windows.html#shebang-lines

我的 PATH 中也有脚本文件夹,所以类似于testing.py"目前正在命令行中工作.

I also have the script folder in my PATH, so something like "testing.py" is currently working from the command line.

根据文档和本教程,https://dbader.org/blog/how-to-make-command-line-commands-with-python

如果我在 PATH 和上面的shebang中有正确的路径,我应该能够通过测试"来唤起我的Python脚本.但是,如果不添加.py",我似乎无法运行脚本.

I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".

推荐答案

不,Windows 不支持 shebang 行.

No, Windows does not support shebang lines.

您链接的文档与 Python 安装的 py 启动器有关,它可以解释各种 shebang 行以选择 Python 版本来运行脚本.

The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.

setuptools 能够为您的 Python 脚本生成包装器 .exe,但它有点复杂并且已经假设你有一个带有 setup.py 的包等等.

setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.

在本地,如果您真的真的需要这个,您可能可以将 .py 添加到 PATHEXT 环境变量,所以 Windows 命令行查找 .py 就像查找 .exes(以及其他各种;当前现代默认值为 .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC).但是,这自然不会扩展到分发应用程序,因为您的所有用户也需要进行设置.

Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.

我的建议是坚持使用那种无聊的旧 python testing.py,真的.

My recommendation is to stick with just that boring old python testing.py, really.

这篇关于在 Windows 中使用 #!/usr/bin/env python3 shebang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆