如何使可执行文件在Shell中使用-Python [英] How to make an executable to use in a shell - Python

查看:107
本文介绍了如何使可执行文件在Shell中使用-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,我想知道如何使它可执行.换句话说,如何使用像bash这样的shell来运行它.

I have a Python script and I was wondering how I can make it executable; in other words how can I run it by using a shell like bash.

我知道第一件事是坚持在第一行#! /usr/bin/env python上,但是然后我是否需要例如将功能按特定顺序排列(即,主要功能在顶部还是底部).我还需要保留python文件扩展名.py(我可以只调用函数Dosomething吗?).

I know the first thing is to stick on the first line #! /usr/bin/env python but then do I need for example the functions to be in a specific order (i.e., the main one at the top or the bottom). What's more do I need to keep the extension .py for my python file (can I just call the function Dosomething?).

简而言之,您能提供一个简单的指南,使某人成为Python文件可执行文件时必须考虑的要点吗?

To be short, could you provide a simple guide, the important points someone has to take into account to make a Python file executable?

推荐答案

这就是我制作可执行脚本的方式.它不考虑eggs或类似的东西.这只是我希望能够执行的简单脚本.我假设您使用的是Linux.

This is how I make an executable script. It doesn't take eggs or anything like that into account. It's just a simple script that I want to be able to execute. I'm assuming you are using linux.

#! /usr/bin/env python
import sys


def main():
    #
    # Do something ... Whatever processing you need to do, make it happen here.
    # Don't shove everything into main, break it up into testable functions!
    #
    # Whatever this function returns, is what the exit code of the interpreter,
    # i.e. your script, will be.  Because main is called by sys.exit(), it will
    # behave differently depending on what you return.
    # 
    # So, if you return None, 0 is returned.  If you return integer, that 
    # return code is used.  Anything else is printed to the console and 1 (error) 
    # is returned.
    #
    if an_error_occurred:
        return 'I\'m returning a string, it will be printed and 1 returned'

    # Otherwise 0, success is returned.
    return 0

# This is true if the script is run by the interpreter, not imported by another
# module.
if __name__ == '__main__':
    # main should return 0 for success, something else (usually 1) for error.
    sys.exit(main())

现在,如果正确设置了权限,则可以执行此脚本.

Now, if you're permissions are set correctly, you can execute this script.

要实现的一件事是,在处理脚本时,每行都在解释器中执行.不管处理器如何获取"它都是如此.那就是将脚本作为模块导入并作为脚本执行,实际上两者都工作相同,因为它们都执行模块的每一行.

One thing to realize is as your script is processed each line is executed in the interpreter. This is true, regardless of how the processor "gets it". That is importing a script as a module and executing it as a script essentially both work the same, in that they both execute each line of the module.

一旦您意识到脚本只是在运行时执行,您就会意识到函数的顺序无关紧要.函数声明是函数声明.这是您 调用 重要的功能的时间.

Once you realize your script is simply executing as it runs, you realize that the order of functions don't matter. A function declaration is a function declaration. It's when you call the function that matters.

因此,通常来说,脚本的布局如下所示:

So, in general, the layout of your script looks like this

def func1():
    pass
def func2():
    pass
def main():
    return 0

if __name__ == '__main__':
    sys.exit(main())

首先创建要使用的功能,然后再使用它们.希望对您有所帮助.

You create the functions you want to use first, then you use them. Hope it helps.

这篇关于如何使可执行文件在Shell中使用-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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