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

2019-08-05 05:29发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

You'll also have to give it execution rights:

chmod u+x yourfile.py


回答4:

Delete the first space. That is,

#!/usr/bin/env python

Should be the very first line of your file. Then, make sure you make set the permisions for the the file to executable with:

chmod u+x your_script.py

Python scripts execute in sequential order. If you have a file filled with functions, common practice is to have something that looks like this at the very end of your file:

if __name__ == '__main__':
    main()

Where main() starts execution of your script. The reason we do it this way, instead of a bare call to main() is that this allows you to make your script act like a module without any modification; if you just had a single line that called main(), your module would would execute the script's main. The if statement just checks if your script is running in the __main__ namespace, i.e., it is running as a script.



回答5:

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.



回答6:

  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!"


回答7:

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