shell执行指定python文件的方法_如何使可执行文件在Shell中使用-Python

本文详细介绍了如何将Python脚本转换为可直接通过shell(如bash)运行的可执行文件。关键步骤包括在脚本开头添加shebang(#!/usr/bin/env python),然后确保主要功能位于`if __name__ == '__main__':`块中。脚本的函数定义顺序并不重要,只要在需要的地方调用即可。正确设置权限后,你可以直接运行这个Python脚本,而无需通过解释器。
摘要由CSDN通过智能技术生成

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.

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?).

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值