直接从命令行运行 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20318158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run python script directly from command line
提问by confused
#!/usr/bin/env python
I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.pyas long as I have the above line at the top of the script. Is this correct?
我把它放在脚本的顶部。我已经看到应该使脚本可以从命令行运行,而无需python programname.py. 除非我误解了我应该能够使用programname.py,只要我在脚本顶部有上面的行。这样对吗?
It isn't working for me I just get an error indicating that I would have to use pythonat the beginning of the 'call'.
它对我不起作用,我只是收到一个错误,表明我必须python在“通话”开始时使用。
回答by Aaron Hall
Universal running of Python scripts
Python 脚本的通用运行
You can pretty much universally run without the shebang (#!) with
你几乎可以在没有shebang(#!)的情况下普遍运行
python myscript.py
Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):
或者几乎等效(它将当前目录放在您的路径上并执行名为 的模块myscript)(最好这样做!):
python -m myscript
from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).
从命令行,只要您安装了 Python 并且在您的路径环境变量上(即设置为 run with python,如果安装,通常就是这种情况)。
Shebangs (#!) are a Unix thing.
Shebangs ( #!) 是 Unix 的东西。
The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.
shebang,正如您所使用的,通常用于在 Unix 平台(通常是 Apple 或 Linux)上运行。Windows 通常需要 cygwin 才能使用 shebang。
You can usually default to whatever python is available on your system path with:
您通常可以默认使用系统路径上可用的任何 python:
#!/usr/bin/env python
Assuming you're on a Unix, you might try other locations for your python setup, like:
假设您使用的是 Unix,您可以尝试其他位置进行 Python 设置,例如:
#!/usr/bin/python
Muddling through
混日子
You can see what python you're currently using by using the unix whichcommand, so if you want to see where your python is coming from, use this command:
您可以使用 unixwhich命令查看您当前使用的 python ,因此如果您想查看您的 python 来自哪里,请使用以下命令:
which python
or on Windows (cygwin probably can run the shebang):
或在 Windows 上(cygwin 可能可以运行 shebang):
where python
On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod
在 Linux/Unix 上,您还需要执行权限以这种方式运行文件。使用 chmod
chmod +x myscript.py
(chmod also may apply to Cygwin in Windows)
(chmod 也可能适用于 Windows 中的 Cygwin)
If you're not running as root, you may require sudo, and that would be
如果您不是以 root 身份运行,则可能需要sudo,那就是
sudo chmod +x myscript.py
And then attempt to run (within the same directory) with
然后尝试运行(在同一目录中)
./myscript.py
回答by jpwagner
make the file executable
使文件可执行
sudo chmod +x /path/to/file.py
and then from the same directory as file.py:
然后从与 file.py 相同的目录:
./file.py

