如何从 bash shell 执行 .py 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14786186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:30:57  来源:igfitidea点击:

how to execute .py script from bash shell

pythonbashshell

提问by aaron

I open a terminal and cd into my documents where I have my .py file called trianglearea.py which is a very basic program that I am trying to execute. I type /Documents$ python trianglearea.py into the shell and nothing happens; bash prompts me for another command as if what I just prompted doesn't exist. I try ./trianglearea.py but I get a syntax error because I have not entered interactive python and bash doesn't understand my python script. If I go to the file gui style and double click and say run in terminal a window flashes and disappears but that is not what I want. I want to have my little program run in interactive python so I can enter stuff and actually use my function. This is all I have in the.py file

我打开一个终端,然后 cd 进入我的文档,其中有我的 .py 文件,名为 trianglearea.py,这是我正在尝试执行的一个非常基本的程序。我在 shell 中输入 /Documents$ python trianglearea.py ,但没有任何反应;bash 提示我输入另一个命令,就好像我刚刚提示的不存在一样。我尝试 ./trianglearea.py 但我收到一个语法错误,因为我没有输入交互式 python 并且 bash 不理解我的 python 脚本。如果我转到文件 gui 样式并双击并说在终端中运行,一个窗口会闪烁并消失,但这不是我想要的。我想让我的小程序在交互式 python 中运行,这样我就可以输入内容并实际使用我的函数。这是我在 .py 文件中的全部内容

def area(base,height):
    ''' (number, number) - number
    Return area of a triangle with dimensions base * height

    >>>>area(10,5)
    25.0
    '''
    return base*height/2

I know this shouldn't be that hard but it has stumped me.

我知道这不应该那么难,但它难倒了我。

采纳答案by Grijesh Chauhan

Suppose if your script file name is mycode.pythen you can use python -i mycode.py:

假设如果您的脚本文件名是mycode.py那么您可以使用python -i mycode.py

$ python -i mycode.py
>>> area(10, 5)   

-iWhen a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. refrence

-i当脚本作为第一个参数传递或使用 -c 选项时,执行脚本或命令后进入交互模式。参考

回答by Burhan Khalid

The reason nothing happens is because you haven't called your method in your script, so the file simply defines the method and then quits. This is a quick fix:

什么都没发生的原因是因为您没有在脚本中调用您的方法,因此该文件只是定义了该方法然后退出。这是一个快速修复:

def area(base,height):
    ''' (number, number) - number
    Return area of a triangle with dimensions base * height

    >>>>area(10,5)
    25.0
    '''
    return base*height/2

print area(10,5) # calling the method

The proper way to fix this is to add this check:

解决此问题的正确方法是添加此检查:

if __name__ == '__main__':
    print area(10,5)

When Python scripts are run from the command line, the special variable __name__is set to the string '__main__'(see this answerfor more). That if statement essentially says, "If I am run from the command line, execute the method area with the arguments 10 and 5 and print the results."

从命令行运行 Python 脚本时,特殊变量__name__设置为字符串'__main__'(有关更多信息,请参阅此答案)。if 语句本质上是说,“如果我从命令行运行,则使用参数 10 和 5 执行方法区域并打印结果。”

Finally, to make sure your script can be run from the prompt like this ./myscript.py, you need to tell your shell how to execute it. This is done by adding a shebang lineto the top of your file.

最后,为了确保您的脚本可以像这样从提示中运行./myscript.py,您需要告诉您的 shell 如何执行它。这是通过在文件顶部添加一条shebang 行来完成的。

Combining all that, your file looks like:

结合所有这些,您的文件如下所示:

#!/usr/bin/python

def area(base,height):
        ''' (number, number) - number
        Return area of a triangle with dimensions base * height

        >>>>area(10,5)
        25.0
        '''
        return base*height/2

if __name__ == '__main__':
    print area(10,5)