Python First Steps
Python First Steps
Once youve saved this text file, you can ask Python to run it by listing its full filename
as the first argument to a python command, typed at the system shell prompt:
% python script1.py
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
Again, you can type such a system shell command in whatever your system provides
for command-line entrya Windows Command Prompt window, an xterm window,
or similar. Remember to replace python with a full directory path, as before, if your
PATH setting is not configured.
If all works as planned, this shell command makes Python run the code in this file line
by line, and you will see the output of the scripts three print statementsthe name
of the underlying platform, 2 raised to the power 100, and the result of the same string
repetition expression we saw earlier (again, more on the last two of these in Chapter 4).
If all didnt work as planned, youll get an error messagemake sure youve entered
the code in your file exactly as shown, and try again. Well talk about debugging options
in the sidebar Debugging Python Code on page 67, but at this point in the book
your best bet is probably rote imitation.
Because this scheme uses shell command lines to start Python programs, all the usual
shell syntax applies. For instance, you can route the output of a Python script to a file
to save it for later use or inspection by using special shell syntax: % python script1.py > saveit.txt
In this case, the three output lines shown in the prior run are stored in the file
saveit.txt instead of being printed. This is generally known as stream redirection; it
works for input and output text and is available on Windows and Unix-like systems.
It also has little to do with Python (Python simply supports it), so we will skip further
details on shell redirection syntax here.
If you are working on a Windows platform, this example works the same, but the system
prompt is normally different:
C:\Python30> python script1.py
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
As usual, be sure to type the full path to Python if you havent set your PATH environment
variable to include this path or run a change-directory command to go to the path:
D:\temp> C:\python30\python script1.py
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
On all recent versions of Windows, you can also type just the name of your script, and
omit the name of Python itself. Because newer Windows systems use the Windows
Registry to find a program with which to run a file, you dont need to name python
on the command line explicitly to run a .py file. The prior command, for example, could
be simplified to this on most Windows machines:
D:\temp> script1.py
Finally, remember to give the full path to your script file if it lives in a different directory
from the one in which you are working. For example, the following system command
line, run from D:\other, assumes Python is in your system path but runs a file located
elsewhere:
D:\other> python c:\code\otherscript.py
If your PATH doesnt include Pythons directory, and neither Python nor your script file
is in the directory youre working in, use full paths for both:
D:\other> C:\Python30\python c:\code\otherscript.py
The special line at the top of the file tells the system where the Python interpreter lives.
Technically, the first line is a Python comment. As mentioned earlier, all comments in
Python programs start with a # and span to the end of the line; they are a place to insert
extra information for human readers of your code. But when a comment such as the
first line in this file appears, its special because the operating system uses it to find an
interpreter for running the program code in the rest of the file.
Also, note that this file is called simply brian, without the .py suffix used for the module
file earlier. Adding a .py to the name wouldnt hurt (and might help you remember that
this is a Python program file), but because you dont plan on letting other modules
import the code in this file, the name of the file is irrelevant. If you give the file executable
privileges with a chmod +x brian shell command, you can run it from the operating
system shell as though it were a binary program:
% brian
The Bright Side of Life...
A note for Windows users: the method described here is a Unix trick, and it may not
work on your platform. Not to worry; just use the basic command-line technique explored
earlier. List the files name on an explicit python command line:*
* As we discussed when exploring command lines, modern Windows versions also let you type just the name
of a .py file at the system command linethey use the Registry to determine that the file should be opened
with Python (e.g., typing brian.py is equivalent to typing python brian.py). This command-line mode is
similar in spirit to the Unix #!, though it is system-wide on Windows, not per-file. Note that some
programs may actually interpret and use a first #! line on Windows much like on Unix, but the DOS system
shell on Windows simply ignores it.
C:\misc> python brian
The Bright Side of Life...
In this case, you dont need the special #! comment at the top (although Python just
ignores it if its present), and the file doesnt need to be given executable privileges. In
fact, if you want to run files portably between Unix and Microsoft Windows, your life
will probably be simpler if you always use the basic command-line approach, not Unixstyle
scripts, to launch programs.
When coded this way, the env program locates the Python interpreter according to your
system search path settings (i.e., in most Unix shells, by looking in all the directories
listed in the PATH environment variable). This scheme can be more portable, as you
dont need to hardcode a Python install path in the first line of all your scripts.
Provided you have access to env everywhere, your scripts will run no matter where
Python lives on your systemyou need only change the PATH environment variable
settings across platforms, not in the first line in all your scripts. Of course, this assumes
that env lives in the same place everywhere (on some machines, it may be
in /sbin, /bin, or elsewhere); if not, all portability bets are off!