直接从命令行运行python脚本

时间:2022-10-18 14:30:39
#!/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.py as long as I have the above line at the top of the script. Is this correct?

我把它放在脚本的顶部。我已经看到,这应该可以使脚本在命令行中运行,而不需要使用python编程名.py。除非我误解了,否则我应该可以使用程序名。只要我在脚本顶部有上面的一行就行。这是正确的吗?

It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.

它不是为我工作,我只是得到一个错误指示,我必须在“调用”开始时使用python。

2 个解决方案

#1


21  

Universal running of Python scripts

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并安装了path环境变量(例如,设置为使用Python运行,如果安装了Python,通常就是这样)。

Shebangs (#!) are a Unix thing.

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平台上运行(通常是苹果或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 which command, so if you want to see where your python is coming from, use this command:

您可以通过使用unix which命令查看当前使用的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上,您还需要执行perms来以这种方式运行文件。使用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 

#2


6  

make the file executable

使文件可执行

sudo chmod +x /path/to/file.py

and then from the same directory as file.py:

然后从与file.py相同的目录中:

./file.py

#1


21  

Universal running of Python scripts

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并安装了path环境变量(例如,设置为使用Python运行,如果安装了Python,通常就是这样)。

Shebangs (#!) are a Unix thing.

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平台上运行(通常是苹果或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 which command, so if you want to see where your python is coming from, use this command:

您可以通过使用unix which命令查看当前使用的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上,您还需要执行perms来以这种方式运行文件。使用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 

#2


6  

make the file executable

使文件可执行

sudo chmod +x /path/to/file.py

and then from the same directory as file.py:

然后从与file.py相同的目录中:

./file.py