Making scripts executable
Normally, executing a Python program requires typing python <program>.py. However, it is possible to make Python programs self-executing so they don't require typing python as the calling command.
How to do it...
- On *NIX systems, putting
#!/usr/bin/env python as the first line of a program allows the program to be executable by referencing the location of Python on the user'sPATH. Of course, this assumes Python is on thePATH; if not, then the program will have to be invoked like normal. - After this has been added to the program, the file itself needs to be modified to make it executable, that is,Â
$ chmod +x <program>.py. - If you are using a terminal program that displays files and directories in different colors depending on their modes, running the command
lson the directory where the file is located should show it with a different color than non-executable files. - To execute the program, simply type
./<program>.pyand the program will execute without...