-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbook019.html
65 lines (65 loc) · 4.56 KB
/
book019.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="hevea 2.09" />
<link rel="stylesheet" type="text/css" href="book.css" />
<title>Python Programming on Macintosh</title>
</head>
<body>
<a href="book018.html"><img src="previous_motif.gif" alt="Previous" /></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up" /></a>
<a href="book020.html"><img src="next_motif.gif" alt="Next" /></a>
<hr />
<h1 class="chapter" id="sec198"><span class="c006">Appendix B  Python Programming on Macintosh</span></h1>
<p><span class="c006">In this appendix, we walk through a series of steps
so you can run Python on Macintosh. Since Python is
already included in the Macintosh Operating system, we only
need to learn how to edit Python files and run Python programs
in the terminal window.</span></p><p><span class="c006">There are many approaches you can take to editing and running
Python programs, and this is just one approach we have found
to be very simple.</span></p><p><span class="c006">First, you need to install a programmer editor. You
do not want to use TextEdit or Microsoft Word to edit
Python programs. Programs must be in "flat-text" files
and so you need an editor that is good at
editing text files.</span></p><p><span class="c006">Our recommended editor for Macintosh is TextWrangler which
can be downloaded and installed from:</span></p><p><span class="c002">http://www.barebones.com/products/TextWrangler/</span></p><p><span class="c006">To create a Python program, run
<span class="c009">TextWrangler</span> from your <span class="c009">Applications</span> folder.</span></p><p><span class="c006">Let’s make our first Python program be:</span></p><pre class="verbatim"><span class="c004">print 'Hello Chuck'
</span></pre><p><span class="c006">Except that you should change it to be your name.
Save the file in a folder on your Desktop named
<span class="c001">py4inf</span>. It is best to keep your folder names short
and not to have any spaces in your folder or file name.
Once you have made the folder, save the file
into <span class="c001">Desktop\py4inf\prog1.py</span>.</span></p><p><span class="c006">Then run the <span class="c009">Terminal</span> program. The easiest way is to
press the Spotlight icon (the magnifying glass) in the upper
right of your screen, enter “terminal”, and launch the
application that comes up.</span></p><p><span class="c006">You start in your “home directory”. You can see the current
directory by typing the <span class="c001">pwd</span> command in the terminal window.</span></p><pre class="verbatim"><span class="c004">67-194-80-15:~ csev$ pwd
/Users/csev
67-194-80-15:~ csev$
</span></pre><p><span class="c006">you must be in the folder that contains your Python program
to run the program. Use the <span class="c001">cd</span> command to move to a new
folder and then the <span class="c001">ls</span> command to list the files in the
folder.</span></p><pre class="verbatim"><span class="c004">67-194-80-15:~ csev$ cd Desktop
67-194-80-15:Desktop csev$ cd py4inf
67-194-80-15:py4inf csev$ ls
prog1.py
67-194-80-15:py4inf csev$
</span></pre><p><span class="c006">To run your program, simply type the <span class="c001">python</span> command followed
by the name of your file at the command prompt and press enter.</span></p><pre class="verbatim"><span class="c004">67-194-80-15:py4inf csev$ python prog1.py
Hello Chuck
67-194-80-15:py4inf csev$
</span></pre><p><span class="c006">You can edit the file in TextWrangler, save it, and then switch back
to the command line and execute the program again by typing
the file name again at the command-line prompt.</span></p><p><span class="c006">If you get confused in the command-line window, just close it
and open a new one.</span></p><p><span class="c006">Hint: You can also press the “up-arrow” in the command line to
scroll back and run a previously entered command again.</span></p><p><span class="c006">You should also look in the preferences for TextWrangler and set it
to expand tab characters to be four spaces. It will save you lots
of effort looking for indentation errors.</span></p><p><span class="c006">You can also find further information on editing and running
Python programs at <span class="c001">www.py4inf.com</span>.</span></p><span class="c005">
</span><hr />
<a href="book018.html"><img src="previous_motif.gif" alt="Previous" /></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up" /></a>
<a href="book020.html"><img src="next_motif.gif" alt="Next" /></a>
</body>
</html>