Running Python Scripts from the Command Line
In Review
1. Introduction
You've seen how to run Python scripts from inside Wing and inside ArcGIS. When you are processing large numbers of files in a "batch" mode, you'll probably want to run them from the command line. The command line is the old interface that predates windows-based or GUI interfaces. It dates back to computer systems form the 1970's including VAX and CPM. While command line interfaces are largely replaced by GUIs for regular users, command line interfaces offer advantages to us developers because they are much easier to create and allow us to just printout lots of information on what our scripts are doing.
2. Using the Python Command Interpreter
Using the Command Prompt (going back to the 80's)
You can enter the command prompt on MS-Windows by clicking on "Start" and then click on "Run". If you do not see "Run" you can also go do "Start->Programs->Accessories->CommandPrompt". When you see a text box, type "cmd" for "command" and press return. You should see a black window appear with a prompt something like "C:/>". This shows that you are at the "C:" drive and can now type commands into the command line and see them execute.
The command prompt is what is left of the old MS-DOS "command line" interface that we used to talk to computers before the Macintosh made Graphical User Interfaces or GUIs popular. The command line is definitely a throw-back but it still is important and very vaulable for running batch jobs.
A simple command is "dir" which stands for "directory" and will list the contents of the current directory. Type it now and see that the contents are the same as if you opened the "C:" drive in windows. Another command is "cd" which stands for "Change Directory". You can change the directory by just typing "cd DirectoryPath" into the command prompt. Replace "DirectoryPath" with either a full path or the name of a folder in the current folder to go into that folder. You can type "cd .." to "up" or "out of" the current directory.
There are a number of commands that might be helpful and you can see them all by typing "help<Enter>" into the command prompt.
The Python Command Prompt
Use "cd" to change your directory to the folder with the current version of Python you want to use (i.e. C:/Python26/ArcGIS10.0). Type "dir" in this folder and you'll see "python.exe". This is the Python interpreter that we've been running in Wing and ArcGIS all along.
Type "python" into the command line and you'll see the "Python Command Prompt" appear. Type "print("hello world")" and hit return. You should see "hello world" printed on the next line. Type "x=10*3" or the like, and hit return. You should see the result printed on the next line. You're actually executing Python scripts one line at a time just as we do when we "single-step" in Wing. You can use this tool to practice Python commands if you wish. To exit Python, type "quit()".
3. Running Scripts from the Command Line
Leaving the command window you opened in the previous section open, open Wing and enter the following text and save it to a file.
print("Hello World")
Once you're in the folder that contains "python.exe" it's easy to run a script. Just type"python <full path to the script>" and you'll see the "print" output appear below the command. Try that now for the script you just created. You should see the "Hello World" appear in the window. You've now run a Python script from the command line. Try some other print statements and some math in the script and see what the output looks like.
Let's make a much more interesting script. In Wing, enter the following code to printout a large number of values.
Count=0 while (Count<1000000): print(Count) Count+=1
If you run this code in the Wing IDE, you'll see a stream of numbers go by rather quickly. Next, run this script in a command line window. As it's running type "<ctrl> S" and the scrolling will pause. This is a "control key" command and there are a number of them that will be helpful:
- <ctrl> S - Pause
- <ctrl> Q - Resume
- <ctrl> C - Break (sometimes you have to do it twice)
- <ctrl> Z - Break (some programs use Z instead of C)
Debugging
The natural method to debug scripts to use "print()" function calls. This is easy and works great for small number of files and a small number of steps in the script. When scripts start to output thousands of lines, you can add "> output.txt" to the end of the command on the command line and all of the output you'd normally see in the command line window will go to a text file, in the current folder, named "output.txt". You can then open this file and examine it for problems.
4. Getting Parameters Into a Command-Line Script
You can pass parameters from the command line into your Python script. This allows you to create scripts that say, operate on files in different folders, or create sequences with different numbers of values. Enter the following into Wing and save it as a file.
import sys print(sys.argv)
Now enter "python <full path to the script>" into the command line and hit return. You'll see that the path to the script comes into the script as the first element in a list. Now try "python <full path to the script> test" and you'll see that the "test" string comes in as the second element in the list. This is all that is required to pass parameters into Python scripts.
5. Batch Files
Batch files are an old part of the operating system and have largely gone out of use. There is one place when they are handy and that when you have a long list of parameters for a command-line script. A batch file or ".bat" file can be run like a ".exe" from the command line except that batch files contain commands for the command line.
Open Notepad and type the following:
<path to your python.exe> <path to your script> <a parameter>
Then, save this file as "BatchTest.bat" and put it in the same folder as your python script. In the command line, type the name of the batch file and hit return. You should see the command line from the batch file display and then the parameters from the script. The batch file has sent the line of text in the file to the command line for you. This is a handy way to keep from typing long lines of text into the command line prompt.
Note: The batch files have a little language but I would only use them for long command lines as Python can launch subprocesses and is much more flexible and you already know it!