直接从命令行运行python脚本 [英] run python script directly from command line

查看:192
本文介绍了直接从命令行运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/usr/bin/env python

我把在脚本的顶部。我见过,应该使脚本命令行可运行,而不需要蟒蛇programname.py 。除非我的误解,我应该能够使用 programname.py 只要我在脚本的顶部上面一行。它是否正确?

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?

这是不是为我工作我只是得到这说明我将不得不使用蟒蛇在'呼叫'的开头的错误。

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'.

推荐答案

您可以pretty普遍得多无家当运行(#!)以

Universal running of Python scripts

You can pretty much universally run without the shebang (#!) with

python myscript.py

在命令行,只要你有Python的安装和设置与蟒蛇运行(其中,如果已安装,通常会出现这种情况)。

from the command line, as long as you have Python installed and set to run with python (which, if installed, would typically be the case).

的家当,如你使用它,通常对于Unix平台(通常是苹果或Linux)上运行。窗户通常需要cygwin的使用家当。

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.

您通常可以默认为任何蟒蛇可与您的系统路径:

You can usually default to whatever python is available on your system path with:

#!/usr/bin/env python

假设你是在Unix下,你可以尝试其他位置为您Python安装,如:

Assuming you're on a Unix, you might try other locations for your python setup, like:

#!/usr/bin/python

到混混

您可以看到您目前使用的是什么蟒蛇使用UNIX 命令,所以如果你想看到你的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:

which python

或Windows(Cygwin的可能可以运行家当):

or on Windows (cygwin probably can run the shebang):

where python

在Linux / Unix上,你需要执行烫发运行该文件为好,以这种方式。使用chmod

On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod

chmod +x myscript.py

(文件模式也适用于Cygwin的Windows环境)

(chmod also may apply to Cygwin in Windows)

如果你不是以root身份运行,则可能需要须藤,这将是

If you're not running as root, you may require sudo, and that would be

sudo chmod +x myscript.py

和再尝试运行(相同的目录内)以

And then attempt to run (within the same directory) with

./myscript.py 

这篇关于直接从命令行运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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