How to Executing Python Code
Before we can really explore the Python language we need to know how to execute Python code. We will show this by reviewing a tiny example program that is just one line long.
We must use a plain text editor for working with Python ﬁles.★ On Windows it is possible to use Notepad, but IDLE includes a suitable Python editor designed speciﬁcally for editing Python code: Simply start IDLE and then click File→New Window.
We will type the following line into a ﬁle, called hello.py:
print "Hello World"
Note that no semicolon is necessary: In Python newline acts as a statement separator. Also, we do not need a newline, “\n”, in the string, since print automatically adds a newline unless we suppress it with a trailing comma.
Assuming that we have saved the code in the ﬁle hello.py (in the directory C:\pyqt\chap01 if using Windows), we can start up a console (click Start→All Pro- grams→Accessories→Console on Windows XP—sometimes Console is called Command Prompt; or run Terminal.app from /Applications/Utilities on Mac OS X), change to that directory, and execute the program like this:
C:\>cd c:\pyqt\chap01 C:\pyqt\chap01>hello.py
As long as Python is correctly installed, Windows will recognize the .py ﬁle extension and give the ﬁle to python.exe to execute. The program will print “Hello World” on the console as we would expect.★
On Mac OS X and Linux we must explicitly run the interpreter by typing its name and the ﬁle’s name at the console’s prompt, like this:
% python hello.py
This will work providing that Python is installed and in your PATH. Alternatively, for Linux and Mac OS X, we can add an additional “shebang” (shell execute) comment line which tells the operating system to use a Python interpreter, making the hello.py ﬁle two lines long:
#!/usr/bin/env python print "Hello World"
For this to work on Mac OS X and Linux, the ﬁle’s permissions must be set correctly. For example, at the console prompt in the same directory as the ﬁle, enter chmod +x hello.py to make the ﬁle executable.
Python comments start with “#” and continue until the end of the line. This means that it is perfectly safe to add the “shebang” line to all Python programs, since the comment is ignored on Windows but on Linux it tells the operating system to execute the ﬁle using a Python interpreter. Appendix A shows how to associate the Python interpreter with .py and .pyw ﬁles on Mac OS X.
When we speak of executing a Python program, what happens behind the scenes is that Python reads the .py (or .pyw) ﬁle into memory, and parses it, to get a bytecode program that it then goes on to execute. For each module that is imported by the program, Python ﬁrst checks to see whether there is a precom piled bytecode version (in a .pyo or .pyc ﬁle) that has a timestamp which corresponds to its .py ﬁle. If there is, Python uses the bytecode version; otherwise, it parses the module’s .py ﬁle, saves it into a .pyc ﬁle, and uses the bytecode it just generated. So, unlike Java, we don’t have to explicitly bytecode-compile any modules, whether they are supplied with Python or are ones we have written ourselves. And in most Python installations, the supplied modules are compiled as part of the installation process so as to avoid having to compile them whenever a Python application that uses them is run.
Now, It’s easy Python toh?