How to make an executable to use in a shell - Pyth

2019-08-05 05:13发布

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?

7条回答
该账号已被封号
2楼-- · 2019-08-05 05:19

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.

查看更多
你好瞎i
3楼-- · 2019-08-05 05:29
  1. Place #!/usr/bin/python in the first line
  2. You can name your python script anything, like: myPythonScript (No, you do not need to keep .py extension)
  3. chmod +x myPythonScript
  4. Run it: ./myPythonScript

Example: myPythonScript

#!/usr/bin/python
print "hello, world!"
查看更多
ゆ 、 Hurt°
4楼-- · 2019-08-05 05:30

Your code should follow the template

# any functions I want to define, and will be accessible when imported as module  
# or run from command line
...

if __name__ == '__main__':
    # things I want to do only when I run it from the command line
    ...  

If you want to be able to run it without having to use python fileName.py but rather just ./fileName.py then you will want to make the first line of your file

#!/usr/bin/env python

And make the file executable by the user at least

chmod u+x fileName.py

If you do not add a .py extension to your file then it will still be runnable from the command line ... but not importable by other modules.

查看更多
Animai°情兽
5楼-- · 2019-08-05 05:32

The only thing (like you said it) is to include:

#! /bin/env python

on the first line. And is not even mandatory, but recommended. After that, you can just call it writing:

python [filename].py

in a terminal or in a bash file.

查看更多
Root(大扎)
6楼-- · 2019-08-05 05:34

You need to add sha bang as you described, e.g.

#!/usr/bin/python

or

#!/usr/bin/env python

as the first line in your file and you need to make it executable by running

chmod +x Dosomething

You do not need to do anything else, in particular file name may be anything including Dosomething. Your PATH probably doesn't include the directory where the file resides, so you should run it like this (assuming your current working directory is where the file is):

./Dosomething
查看更多
孤傲高冷的网名
7楼-- · 2019-08-05 05:40

You'll also have to give it execution rights:

chmod u+x yourfile.py
查看更多
登录 后发表回答