How To Make An Executable To Use In A Shell - Python
Solution 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.return0# 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
deffunc1():
pass
deffunc2():
pass
defmain():
return0if __name__ == '__main__':
sys.exit(main())
You create the functions you want to use first, then you use them. Hope it helps.
Solution 2:
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.
Solution 3:
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.
Solution 4:
You'll also have to give it execution rights:
chmod u+x yourfile.py
Solution 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.
Post a Comment for "How To Make An Executable To Use In A Shell - Python"