If you're on UNIX, you can easily make a script directly executable. Note that if Python 3.x isn't your default version of Python, you may need to change the python above to python3.1 or something similar to specify that you want to use Python 3.x instead of an earlier default version. Then, if you place your script somewhere on your path (for example, in your bin directory), you can execute it regardless of the directory you're in by typing its name and the desired arguments. On UNIX, you'll have input and output redirection and, if you're using a modern shell, command history and completion. If you're writing administrative scripts on UNIX, a number of library modules are available that you may find useful. These include grp for accessing the group database, pwd for accessing the password database, resource for accessing resource usage information, syslog for working with the syslog facility, and stat for working with information about a file or directory obtained from an os.stat call. In many ways, Python scripts on Mac OS X behave the same way as they do on Linux/ UNIX.
You can run Python scripts from a terminal window exactly the same way as on any UNIX box. But on the Mac, you can also run Python programs from the Finder, either by dragging the script file to the Python Launcher app or by configuring Python Launcher as the default application for opening your script (or, optionally, all files with a .py extension.) There are several options for using Python on a Mac. If you're interested in writing administrative scripts for Mac OS X, you should look at packages that bridge the gap between Apple's Open Scripting Architectures (OSA) and Python. Two such packages are appscript and PyOSA.
If you're on Windows, you have a number of options for starting a script that vary in their capability and ease of use. Unfortunately, none of them are as flexible or powerful as on Linux/UNIX. The easiest way to start a script on Windows is to use its standard document-opening technique. When you installed Python, it should have registered the .py suffix to itself. Verify this by confirming that your .py files are shown with a stylized python icon. If you double-click any .py file, Python is automatically called with this file as its argument. It's also entered onto the Documents list on your Start menu. But you're not able to enter any arguments, and the command window in which the interpreter is opened will close as soon as the script exits. If you want to have the window stay up so you can read the output, you can place the following line at the bottom of your controlling function. This will leave the window up until you press Enter. You can also query the user for any input data you might have desired on the command line. Your current working directory at startup will be the one where your Python interpreter is located.
If you don't want the interpreter window to open (for example, when you're starting a GUI program using Tkinter), you can give the file the suffix .pyw. This will cause it to be opened by pythonw.exe. But if you start a script this way, any output to stdout or stderr will be thrown away. You have more flexibility and the ability to pass in more information to your script if you set it up as a Windows shortcut. Right-click the script, and select either the Create Shortcut or the Send to Desktop as Shortcut option. You can move the shortcut to any location and rename it as desired. By right-clicking it and selecting the Properties option, you can set the directory it starts in, type in arguments that it will be called with, and specify a shortcut key that will call it. Creating the shortcut and then calling this script (by pressing Ctrl-Alt-j or double-clicking its icon) brings up a Python window containing code.
The Python interpreter is implicitly called with the target line (because it has registered for Python files). You can also explicitly put it in. You may do this if you want to also enter options for the interpreter itself. Because you change the selection for the Run line to Minimized from the default Normal window, no MS-DOS window will be brought up, just as when you use the .pyw suffix for a document. There is unfortunately no mechanism for redirecting the input or output for shortcuts. It's possible to start scripts by placing the script on the desktop and typing python script.py arg1 arg2. But as mentioned before the output won't remain visible when the script ends. To see the output after the scripts ends, you similarly have to end it with an input line. More flexibility is available with the Run box. Using the window that opens when you click the Browse button, you can search for scripts residing elsewhere (by selecting All Files for the Files of Type box at the bottom, because this defaults to Programs). This results in the pathname to the script you selected being displayed in the Run box. You have to prefix this with python. Also, clicking the selection button to the right of the text box brings up a history of your past lines of entries, from which you can select a line to edit. The current working directory for any script started from the Run box is the desktop. To run a script from a command window, open a command prompt and navigate to the directory of your Python executable. You can then enter your commands. You don't need to use the -i option because the script will run in your existing window. This is the most flexible of the ways to run a script on Windows because it allows you to use input and output redirection. It's not all that convenient, because you don't have mouse editing, command completion, or the command history that you have in modern UNIX shells.
It also doesn't provide the full power of an executable script on UNIX. It lacks the ability, for example, to navigate into a directory and call a script with a set of filenames from that directory as arguments without either having to place the script in that directory or enter the full pathname of the files or the script. Note that if you're using input or output redirection, the default is for Windows to use the text mode. If you want your script to work with binary data, you either need to call the Python interpreter with the -u option set (python -u script.py) or set the environment variable PYTHONUNBUFFERED=1 to turn off buffering and place it in a binary mode. You can also add the directory where the Python interpreter is to your PATH environment variable. This frees you from having to move into the same directory as the interpreter or refer to it with a full pathname. The easiest way to do this on Windows XP is to right-click the My Computer desktop icon, choose Properties > Advanced, and then click the Environment Variables button. There, you can edit the PATH variable to add the path to the Python executable, usually something like c:\Python31.
Other options are available to explore. If you're familiar with writing batch files, you can wrap your commands in them. A port of the GNU BASH shell comes with the Cygwin tool set. This provides a UNIX-like shell capability for Windows. On Windows, you can edit the environment variables to add .py as a magic extension, making your scripts automatically executable.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Kyle D. at 01052010
1. How to create a basic program in Python
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.