forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfbook019.html
More file actions
101 lines (101 loc) · 4.11 KB
/
cfbook019.html
File metadata and controls
101 lines (101 loc) · 4.11 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="GENERATOR" content="hevea 1.07" />
<title>
Python Programming on Macintosh
</title>
</head>
<body>
<a href="cfbook018.html"><img src="previous_motif.gif" alt="Previous" /></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up" /></a>
<a href="cfbook020.html"><img src="next_motif.gif" alt="Next" /></a>
<hr />
<h1><font color="black"><a name="htoc197">Appendix B</a> Python Programming on Macintosh</font></h1>
<font color="black">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 need to
learn how to edit Python files and run Python programs
in the terminal window.<br />
<br />
There approaches you can take to editing and running
Python programs, and this is just one
approach we have found to be very simple.<br />
<br />
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.<br />
<br />
Our recommended editor for Macintosh is TextWrangler which
can be downloaded and installed from:<br />
<br />
<tt>http://www.barebones.com/products/TextWrangler/</tt><br />
<br />
To create a Python program, run from
<b>TextWrangler</b> from your <b>Applications</b> folder.<br />
<br />
Lets make our first Python program be:
</font><pre><font size="4" color="blue">
print 'Hello Chuck'
</font></pre><font color="black">Except that you should change it to be your name.
Lets save the file in a folder on your Desktop named
<tt>py4inf</tt>. 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 <tt>Desktop\py4inf\prog1.py</tt>.<br />
<br />
The run the <b>Terminal</b> program. The easiest way is to
press the Spotlight icon (the magnifying glass) in the upper
right of your screen and enter "terminal" and launch the
application that comes up.<br />
<br />
You start in your "home directory". You can see the current
directory by typing the <tt>pwd</tt> command in the terminal window.
</font><pre><font size="4" color="blue">
67-194-80-15:~ csev$ pwd
/Users/csev
67-194-80-15:~ csev$
</font></pre><font color="black">We must be in the folder that contains your Python program
to run the program. We user the <tt>cd</tt> command to move to a new
folder and then the <tt>ls</tt> command to list the files in the
folder.
</font><pre><font size="4" color="blue">
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$
</font></pre><font color="black">To run your program, simply type the <tt>python</tt> command followed
by the name of your file at the
command prompt and press enter.
</font><pre><font size="4" color="blue">
67-194-80-15:py4inf csev$ python prog1.py
Hello Chuck
67-194-80-15:py4inf csev$
</font></pre><font color="black">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.<br />
<br />
If you get confused in the command line window - just close it
and start a new one.<br />
<br />
Hint: You can also press the "up-arrow" in the command line to
scroll back and run a previously entered command again.<br />
<br />
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.<br />
<br />
You can also find further information on editing and running
Python programs at <tt>www.py4inf.com</tt>.</font><br />
<br />
<hr />
<a href="cfbook018.html"><img src="previous_motif.gif" alt="Previous" /></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up" /></a>
<a href="cfbook020.html"><img src="next_motif.gif" alt="Next" /></a>
</body>
</html>