Computational Linguistics & Phonetics Computational Linguistics & Phonetics Fachrichtung 4.7 Universität des Saarlandes

Running Python Programs

Concepts

Each computer needs to run an operating system such as Linux or Windows. It's the first 'program' that your computer loads after you switched it on. The operating system starts and manages all the processes and programs that you execute on your computer. In many cases, you start programs by clicking on some icon and you don't really care how that icon 'tells' your computer to start your program. The operating system needs to 'hear' a textual command that tells it to start a program. For instance, if we want to start an internet browser, we can simply say 'firefox.exe' to the computer, and it will start Mozilla Firefox. (It's not really polite but effective =)). Your click on the Firefox icon invokes exactly this communication with the operating system.

Operating systems provide a very simple program that lets you directly talk to your computer (operating system) using such textual commands. In Linux, this program is called terminal or shell. In Windows, it's called command line interface and you can start it by clicking on the Start Button, typing cmd and then clicking on the cmd program (shown with the little black icon). Picture of Windows Start Button

The operating system's terminal or command line interface is not to be confused with Python's shell. They look very similar once you opened them (a black window waiting for you to enter some text, and if you hit enter, something happens). Python's shell provides an interactive session with the Python interpreter of the Python Virtual Machine (PVM). As a reminder, the PVM is the program that executes your Python programs. The Python shell is an interface to this interpreter, you can enter Python code statements, and when you hit enter, the interpreter executes the statements one-by-one. The terminal or command line interface expects commands like 'start this program' or 'show me all the files of the current directory'. The Python shell only understands Python code statements. When you start the IDLE (the tool / program that helps you to write Python code), you get two windows, one contains the Python shell and one is a simple text editor, or source code editor, where you can work on programs that can be executed later (by pressing F5).

The terminal / cmd session is executed inside a directory (folder). You can see the location in the front of the command prompt:
Windows: C:\Users\anne>
Linux: afried@login:~/Websites/python1-12$

You can change this directory using the command cd. You can go to a subfolder of the current folder using cd subfolder-name and you can navigate to the parent folder by typing cd .. (two dots). To see a list of subfolders, type dir in Windows and ls on Linux machines. (More Windows commands are listed here.)

Interactive Mode vs. Executing Scripts

The Python shell allows you to work with the PVM in an interactive way; you enter code statement by code statement and the interpreter executes the statements immediately. This is useful if you want to try out little things in Python. However, if you are asked to write a larger program, this is clearly impractical. A better solution is to write scripts, i.e., files containing source code. Here, you also write one statement per line, but Python can execute them all together (in the correct order, of course) when you execute the whole script. I strongly recommend using this mode, even if you are trying out little things, because you can save your source code files, add a lot of comments and look up what you wrote the week or months before. And if you are programming some software, it will be required to create source code files. The Python shell is just for debugging.

When you are using IDLE, your life is very easy. You use the source code editor, press F5 and Python executes your script. The results, i.e., everything you print in your program, is shown in the Python shell window. You can even type variable names that were used in the script and see the value after the execution of the program.

There is also another way to execute your scripts: You can use the terminal or command line interface to do that. Open the terminal or cmd. If you type python (or python3), a Python shell session will be started. No new window is opened, so now your Python shell is in the window of the terminal/cmd. If you want to close the Python shell and get back to the terminal or cmd, simply type exit().
But we wanted to execute a script, not start the interactive Python shell. In this case, you type the following into your terminal or cmd: python name_of_your_script.py
(In case you are using Windows and this doesn't work, check the section below.)

This time, the Python VM is started, executes your script, prints the stuff that your printed inside your Python code on the terminal / cmd, and automatically exits (unless your program contains some statements such as input(), then it waits for user input, and only terminates if the program is really done). If you want to execute a Python script this way, you need to make sure that you are in the same directory as your .py file. You can navigate there using the cd command as described above.

Create a source code file that only has one statement: print("Hello world!"), and execute it using the way described above. You can still edit the source code file using the IDLE editor (this is actually recommended). Just instead of pressing F5, you open a terminal or cmd and execute the program from there.

Command line arguments

Why is it useful to start programs from the terminal or command line? One thing you can do there is to pass command line arguments. A command line argument is just a string that you enter after the name of your script when starting it, and that can be retrieved inside your Python code: python name_of_your_script.py command_line_argument1 command_line_argument2 (and so on). So actually you can pass a list of strings to your program using command line arguments, and you can retrieve them inside your program using sys.argv. You can find a description of how to do this in the lecture slides or here. If you are a beginner, only read the beginning (the first two examples and explanations). These explanations are for Python 2.X, but all you need to change is to maybe call python3 instead of python. Also replace print arg by print(arg).

Windows 7 Environment Variables

If you type python, Windows will look for the python.exe file in the current directory, i.e., the directory that is shown in your cmd. I told you to go to the folder containg your .py file, but python.exe is wherever you installed Python to, probably in C:\Python33 or C:\Python32. Windows cannot search your whole computer for this file, as this might potentially take a long time. But it will search all the directories that are listed in its environment variable called Path. So all you need to do is to add the location of your Python installtion to this environment variable. Instructions can be found here: If you click edit/bearbeiten for the Path environment variable, simply go to the end of the value and add a semicolon and then the path of your Python installation. Do not add any spaces (unless contained within the path to your installation). For example, the value of the Path environment variable may look like this: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Python32