Running Python Programs
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,
i.e., 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. 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 using such textual commands. This program is usually called (technically incorrectly) terminal, (correctly) shell or command line interface. On Windows you can start it by clicking on the Search button, typing cmd
and then clicking on the cmd
program (Command Prompt
).
The operating system's shell is not to be confused with Python's shell. They look very similar once you opened them (a 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. As a reminder, the Python interpreter 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 operation system's shell 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 IDLE (a very minimalistic development environment
that we will use 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 cmd
session is executed inside a directory (folder). You can see the location in the front of the command prompt:
C:\Users\anne>
You can change this directory using the command cd
. You can go to a subfolder of the current folder using cd <foldername>
and you can navigate to the parent folder by typing cd ..
(two dots). To see a list of subfolders, type dir
(Linux and macOS: ls
). More Windows commands are listed here.
Interactive Mode vs. Executing Programs
The Python shell allows you to work with the Python interpreter 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 your code into files. 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 program.
When you are using IDLE, your life is very easy. You use the source code editor to enter your program; once done, you can press F5 and Python executes your program. 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 program and see the value after the execution of the program.
There is also another way to execute your programs: You can use the command line interface to do that. Open the Command Prompt
(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 operation system's shell. If you want to close the Python shell, simply type exit()
. But we wanted to execute a program, not start the interactive Python shell. In this case, you type the following:
python <program>.py
where <program>.py
is a placeholder for the actual name of your program. If this does not work for you, check the section on environment variables below.
This time, the Python interpreter is started, executes your program, prints the stuff that your printed inside your Python code, 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 program 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 program when starting it, and that can be retrieved inside your Python code:
python <program>.py <first command line argument> <second command line argument> ...
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.
Windows 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 Command Promp. I told you to go to the folder containg your .py
file, but python.exe is wherever you installed Python to, probably in C:\Python38
. 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 edit 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