Macintosh‚Å Python 1.5 ‚šŽg‚¤



‚±‚Ģ•¶‘‚ĶApple Macintosh‚ÅPython‚šŽg‚¤‚½‚ß‚Ģ“ü–发‚Å‚ ‚éB‚±‚Ģ•¶‘‚Å‚ĶPythonŒ¾Œź‚»‚Ģ‚ą‚Ģ‚šŠ‰ī‚·‚邱‚Ę‚Ķ‚µ‚Č‚¢BPythonŒ¾Œź‚š’m‚邽‚ß‚É‚ĶAGudo van Rossum (Python‚ĢŒ“’˜ŽŅ)‚É‚ę‚éPythonƒ`ƒ…[ƒgƒŠƒAƒ‹‚š“Ē‚ń‚Å—~‚µ‚¢B‚±‚ĢƒKƒCƒh‚Ķ‚Ł‚Śƒ`ƒ…[ƒgƒŠƒAƒ‹‚Ģ‘ę“ńĶ‚š’u‚«Š·‚¦‚é‚ą‚Ģ‚Å‚ ‚éB‚Ü‚½A‚»‚Ģ‘¼‚ĢMacintosh “Į—L‚Ģ‚±‚Ę‚É‚Ā‚¢‚Ä‚ąG‚ź‚Ä‚¢‚éB

This document is an introduction to using Python on the Apple Macintosh. It does not introduce the language itself, for this you should refer to the Python Tutorial by Guido van Rossum. This guide more-or-less replaces chapter two of the tutorial, and provides some additional material.

‚±‚Ģƒ`ƒ…[ƒgƒŠƒAƒ‹‚ā‚»‚Ģ‘¼‚Ģd—v‚Č•¶‘A—į‚¦‚Īƒ‰ƒCƒuƒ‰ƒŠƒŠƒtƒ@ƒŒƒ“ƒX“™A‚Ķ‚³‚Ü‚“‚Ü‚ČŒ`Ž®ftp://ftp.python.org/pub/python/doc‚©‚ē“üŽč‰Ā”\‚Å‚ ‚éBAdobe Acrobat‚Ģ.pdfƒtƒ@ƒCƒ‹Œ`Ž®‚Ŗ‚Ø‚»‚ē‚­‚±‚ź‚ē‚Ģ•¶‘‚š‚ ‚Č‚½‚ĢMac‚Ģć‚Å“Ē‚ń‚¾‚čAŽ†‚Ģć‚ɏo—Ķ‚µ‚½‚č‚·‚é‚Ģ‚É“K‚µ‚Ä‚¢‚邾‚ė‚¤B
‚
The tutorial, along with other indispensible documentation like the library reference and such, is also available in a number of different formats at ftp://ftp.python.org/pub/python/doc. The Adobe Acrobat .pdf files are probably a good choice for reading or printing the documents from your mac.

‚¢‚Ü‚Ģ‚Ę‚±‚ėA Python‚ĢMac“Į—L‚Ģ‹@”\‚É‚Ā‚¢‚Ä‚Ģ‚ę‚¢ƒ`ƒ…[ƒgƒŠƒAƒ‹‚Ķ‘‚©‚ź‚Ä‚¢‚Č‚¢B‚µ‚©‚µ‚ ‚Č‚½‚Ģ‹ó• ‚š–ž‚½‚·‚½‚߂ɁAMac—pPython‚ĶMacOs Toolbox(quickdraw, sound, quicktime, open scripting,“™)‚š—˜—p‚·‚邽‚ß‚ĢƒCƒ“ƒ^ƒtƒFƒCƒX‚š’ń‹Ÿ‚·‚éB‚Ü‚½Tk, stdwin, •”‘f”A‰ę‘œ‘€ģ“™‚Ģƒ|[ƒ^ƒuƒ‹‚Čƒc[ƒ‹‚ą—˜—po—ˆ‚éBPython‚Ģ—LŒų«‚šŽ¦‚·‚½‚ß‚Ģ‚³‚Ü‚“‚Ü‚ČƒvƒƒOƒ‰ƒ€—į‚Ŗ—pˆÓ‚³‚ź‚Ä‚¢‚éB

There is currently no good tutorial for the mac-specific features of Python, but to whet your appetite: it has interfaces to many MacOS toolboxes (quickdraw, sound, quicktime, open scripting, etc) and various portable toolboxes are available too (Tk, stdwin, complex numbers, image manipulation, etc). Some annotated sample programs are available to give you an idea of Python's power.

PythonƒCƒ“ƒ^ƒvƒŠƒ^‚š‹N“®‚·‚éInvoking the interpreter

ƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ–¼‘O‚ĶƒCƒ“ƒXƒgƒŒ[ƒVƒ‡ƒ“–ˆ‚ɈŁ‚Č‚Į‚Ä‚¢‚邱‚Ę‚Ŗ‚ ‚éB‚ ‚éƒCƒ“ƒXƒgƒŒ[ƒVƒ‡ƒ“‚Å‚ĶPythonFAT(powerpc macs ‚Ø‚ę‚Ń CFM68K ‚ŖƒCƒ“ƒXƒg[ƒ‹‚³‚ź‚½68K macs) ‚ĘŒÄ‚Ī‚ź‚éB’Źķ‚Ģ68kMac‚Å‚ĶPython68K‚ĘŒÄ‚Ī‚ź‚éBm–óŽŅ’:Œ»Ż‚ĢPython1.5.2c1‚Å‚ĶPythonInterpreter‚ĘŒ¾‚¤–¼‘O‚É‚Č‚Į‚Ä‚¢‚éBn
‚±‚ź‚ē‚ĢƒCƒ“ƒ^ƒvƒŠƒ^‚Ķķ‚É"16 ton"ƒAƒCƒRƒ“‚Å‹ę•Ź‚³‚ź‚éBƒAƒCƒRƒ“‚šƒ_ƒuƒ‹ƒNƒŠƒbƒN‚·‚邱‚Ę‚ÅAƒCƒ“ƒ^ƒvƒŠƒ^‚š‘Ī˜bƒ‚[ƒh‚Å‹N“®o—ˆ‚éB

The name of the interpreter may differ on different installations: it may be called PythonFAT (for powerpc macs and 68K macs with CFM68K installed) or Python68K (for 68K macs). It will always be recognizable by the "16 ton" icon, though. You start the interpreter in interactive mode by double-clicking its icon:

ƒCƒ“ƒ^ƒvƒŠƒ^‚Ŗ‹N“®‚·‚é‚ʁAŽŸ‚Ģ‚ꂤ‚ČPython‚Ģƒo[ƒWƒ‡ƒ“ī•ń‚šŠÜ‚Ž•¶Žš—ń‚Ę“ü—Ķ‚š‘£‚·ƒvƒƒ“ƒvƒg‚ŖƒeƒLƒXƒgƒEƒBƒ“ƒhƒE‚ɏo—Ķ‚³‚ź‚éB
 This should give you a text window with an informative version string and a prompt, something like the following:

Python 1.5.1 (#122 Aug 27, 1997)  [CW PPC w/GUSI MSL]
Copyright 1991-1997 Stichting Mathematisch Centrum, Amsterdam
>>>
ƒo[ƒWƒ‡ƒ“ī•ń‚Ģ•¶Žš—ń‚ĶPython‚Ģƒo[ƒWƒ‡ƒ“A‚±‚ĢƒCƒ“ƒ^ƒvƒŠƒ^‚ŖPPCŒü‚Æ‚Å‚ ‚é‚Ģ‚©A68KŒü‚Æ‚Å‚ ‚é‚Ģ‚©‚Ģī•ńA‚³‚ē‚É‚Ī‚ ‚¢‚É‚ę‚Į‚Ä‚Ķ‚±‚ĢƒCƒ“ƒ^ƒvƒŠƒ^‚šģ¬‚·‚é‚Ę‚«‚ÉŽg‚ķ‚ź‚½ƒIƒvƒVƒ‡ƒ“‚Č‚Ē‚Ģī•ń‚š‚ ‚Č‚½‚É’m‚ē‚¹‚Ä‚­‚ź‚éB‚ą‚µAƒoƒO‚š”­Œ©‚µ‚½ź‡‚ ‚é‚¢‚ĶƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ“®ģ‚É‚Ā‚¢‚Ä‹^–ā‚Ŗ‚ ‚鏼‡A‚±‚ź‚ē‚Ģī•ń‚š‚ ‚Č‚½‚ĢƒƒbƒZ[ƒW‚Ģ‚Č‚©‚É“ü‚ź‚Ä‚Ø‚­‚±‚Ę‚Ķ‚ę‚¢l‚¦‚¾‚ė‚¤B

The version string tells you the version of Python, whether it was built for PPC or 68K macs and possibly some options used to build the interpreter. If you find a bug or have a question about how the interpreter works it is a good idea to include the version information in your message.

ƒCƒ“ƒ^ƒvƒŠƒ^‚Ģƒvƒƒ“ƒvƒg‚Ģ‚ą‚Ę‚Å‘Ī˜b“I‚ÉPythonƒRƒ}ƒ“ƒh‚š“ü—Ķ‚·‚邱‚Ę‚Ŗo—ˆ‚éB‘Ī˜b“I‚ČƒRƒ}ƒ“ƒh‚É‚Ā‚¢‚Ä‚ĢŚ×‚ȏī•ń‚Ķƒ`ƒ…[ƒgƒŠƒAƒ‹‚šŒ©‚Ä—~‚µ‚¢B‘Ī˜b“IƒEƒBƒ“ƒhƒE‚ĶA ƒRƒ~ƒ…ƒjƒP[ƒVƒ‡ƒ“ƒc[ƒ‹ƒ{ƒbƒNƒX‚āTelnet‚ĢƒEƒBƒ“ƒhƒE‚Ę‚Ł‚Ś“Æ‚¶‚ꂤ‚É“­‚­B‚ ‚Č‚½‚ĶƒEƒBƒ“ƒhƒE‚ĢÅ‰ŗ•”‚ŃRƒ}ƒ“ƒh‚š“ü—Ķ‚µA‚»‚ĢƒRƒ}ƒ“ƒh“ü—Ķ‚šmreturn] ‚ ‚é‚¢‚Ķ [enter]ƒL[‚Å‹ęŲ‚éBƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ‰ž“š‚ą‚Ü‚½ƒEƒCƒ“ƒhƒE‚Ģ‰ŗ•”‚É•\Ž¦‚³‚ź‚éB“ą—e‚Ķo—Ķ‚Ŗ’Ē‰Į‚³‚ź‚é‚ɏ]‚Į‚ăXƒNƒ[ƒ‹‚µ‚Ä‚¢‚­BƒRƒs[‚Ęƒy[ƒXƒg‚š‚¢‚Ā‚ą‚Ģ‚ꂤ‚ÉŽg‚¤‚±‚Ę‚Ŗo—ˆ‚éA‚µ‚©‚µƒy[ƒXƒg‚ĶƒEƒBƒ“ƒhƒE‚ĢÅ‰ŗ•”‚Å‚Ģ‚Ż‰Ā”\‚Å‚ ‚éB

At the prompt you can type interactive python commands. See the tutorial for more information. The interactive window works more-or-less like a Communication Toolbox or Telnet window: you type commands at the bottom and terminate them with the [return] or [enter] key. Interpreter feedback also appears at the bottom of the window, and the contents scroll as output is added. You can use copy and paste in the normal way, but be sure to paste only at the bottom of the document.

PythonƒXƒNƒŠƒvƒg‚Ģģ¬@Creating Python scripts

PythonƒCƒ“ƒ^ƒvƒŠƒ^‚Ķ‚ ‚Č‚½‚ŖMacintoshƒvƒƒOƒ‰ƒ€‚É‚Ā‚¢‚ÄŠś‘Ņ‚·‚é‚Ģ‚Ę‚ĶˆŁ‚Č‚é‚ā‚č•ū‚Å“®ģ‚·‚éBƒCƒ“ƒ^ƒvƒŠƒ^‚Ķ’P‚ɃCƒ“ƒ^ƒvƒŠƒ^‚Å‚ ‚éB‘g‚Żž‚Ż‚ĢƒGƒfƒBƒ^‚ą‚Č‚¢‚µŠJ”­‚ĢƒTƒ|[ƒg‚ą–³‚¢B]‚Į‚āAPythonƒXƒNƒŠƒvƒg‚šģ¬‚·‚é‚É‚ĶPythonƒCƒ“ƒ^ƒvƒŠƒ^ˆČŠO‚ɃGƒfƒBƒ^‚Ŗ•K—v‚Ę‚Č‚éBÅ‰‚ĢƒXƒNƒŠƒvƒgģ¬‚É‚ĶAƒvƒŒƒCƒ“‚ŃXƒ^ƒCƒ‹‚Ģ–³‚¢ƒeƒLƒXƒg‚š‚Ā‚­‚邱‚Ę‚Ģ‚Å‚«‚éƒGƒfƒBƒ^A‚½‚Ę‚¦‚ĪSimpleText‚Č‚ĒA‚šŽg‚¤‚±‚Ę‚Ŗo—ˆ‚éB[–óŽŅ’FPython1.5.2c‚Å‚Ķ“‡ŠJ”­ŠĀ‹«Python IDE‚ŖPython for Mac‚Ģ”z•tƒLƒbƒg‚ÉŠÜ‚Ü‚ź‚Ä‚¢‚éB]@

The Python interpreter works in a way that is different from what you would expect of a macintosh program: the interpreter is just that: an interpreter. There is no builtin editor or other development support. Hence, to create a Python script you need an external text editor. For a first script you can use any editor that can create plain, unstyled text files, such as SimpleText.

–{Ši“I‚ČPythonƒXƒNƒŠƒvƒg‚ĢŠJ”­‚É‚ĶABBEdit‚āAlpha‚Č‚Ē‚ĢƒvƒƒOƒ‰ƒ€‰Ā”\‚ČƒGƒfƒBƒ^‚šŽg‚¤‚±‚Ę‚š‚Ø‘E‚ß‚·‚éB
BBEdit‚ĶŽ„‚Ģ‚¢‚æ‚Ī‚ńD‚«‚Č‚ą‚Ģ‚Å‚ ‚éB[–óŽŅ’G–óŽŅ‚ĢFavorite‚ĶAlpha‚Å‚ ‚éB”O‚Ģ‚½‚ß] BBEdit‚Ķ—L—æ‚Ģ¤—p”Å‚Ŗ‚ ‚é‚ŖAŠ®‘S‚É‹@”\‚·‚éƒtƒŠ[‚ČBBEdit Lite‚ą‚ ‚éB‚ ‚Č‚½‚Ķ‚±‚ź‚šBareBones ƒTƒCƒg‚©‚ēƒ_ƒEƒ“ƒ[ƒh‚·‚邱‚Ę‚Ŗo—ˆ‚éBƒtƒŠ[‚Č”Å‚Ķ‚ ‚Č‚½‚Ŗ•K—v‚Ę‚·‚é‚Å‚ ‚ė‚¤‚·‚ׂĂĢ‹@”\‚š”õ‚¦‚Ä‚¢‚éB•W€“I‚Č•ŅW‹@”\‚ɉĮ‚¦‚āA‚±‚ĢƒvƒƒOƒ‰ƒ€‚Ķ•””‚Ģƒtƒ@ƒCƒ‹‚É“n‚錟õ‚Č‚Ē‚ĢƒvƒƒOƒ‰ƒ€•ŅW‚É–š—§‚Ā‹@”\‚š”õ‚¦‚Ä‚¢‚éB

For more serious scripts, though, it is advisable to use a programmers editor, such as BBEdit or Alpha. BBEdit is my favorite: it comes in a commercial version but also in a fully-functional free version BBEdit Lite. You can download it from the BareBones site. The free version will probably provide all the functionality you will ever need. Besides the standard edit facilities it has multi-file searches and many other goodies that can be very handy when editing programs.

‚ ‚Č‚½‚ĢD‚Ż‚ĢƒGƒfƒBƒ^‚ŃXƒNƒŠƒvƒg‚šģ‚čo‚µ‚½ŒćA‚»‚ź‚šƒCƒ“ƒ^ƒvƒŠƒ^‚Ƀhƒƒbƒv‚µ‚Ä‚Ż‚ꂤB‚±‚ź‚É‚ę‚Į‚ăCƒ“ƒ^ƒvƒŠƒ^‚Ķ‚»‚ĢƒXƒNƒŠƒvƒg‚ĢŽĄs‚šŠJŽn‚·‚éB‚±‚ĢƒXƒNƒŠƒvƒg‚ĢŽĄsŽž‚É‚ĶAÄ‚ŃƒvƒƒOƒ‰ƒ€‚šo—Ķ‚·‚éƒEƒBƒ“ƒhƒE‚ŖŒ»‚ź‚éBƒXƒNƒŠƒvƒg‚Ŗ•K—v‚Ę‚µ‚Ä‚¢‚鏼‡‚É‚ĶA‚±‚ĢƒEƒBƒ“ƒhƒE‚©‚ēƒf[ƒ^‚š“ü—Ķ‚·‚邱‚Ę‚É‚Č‚éB
’ŹķƒXƒNƒŠƒvƒg‚Ŗ‚»‚ĢŽĄs‚šI‚¦‚é‚ĘƒCƒ“ƒ^ƒvƒŠƒ^‚ĢŠJ‚¢‚½ƒEƒBƒ“ƒhƒE‚ą•Ā‚¶‚ē‚ź‚éB‚±‚ĢU‚é•‘‚¢‚š•ĻX‚·‚é•ū–@‚É‚Ā‚¢‚Ä‚Ķ‰ŗ‚Ģstartup options‚š‚Ż‚Ä—~‚µ‚¢B

After you have created your script in the editor of your choice you drop it on the interpreter. This will start the interpreter executing the script, again with a console window in which the output appears and in which you can type input if the script requires it. Normally the interpreter will close the window and quit as soon as the script is done executing, see below under startup options for a way to change this.

BBEdit‚Å‚ĶPythonƒvƒƒOƒ‰ƒ€‚šƒXƒNƒŠƒvƒg‚š•ŅW‚µ‚Ä‚¢‚éBBEdit‚ĢƒEƒBƒ“ƒhƒE‚©‚ēPythonƒXƒNƒŠƒvƒg‚šŽĄs‚·‚邽‚ß‚Ģ‹@”\Šg’£‚Ŗ‚ ‚éBMac:Tools:BBPy folder.‚š’²‚ׂĂŻ‚Ä‚­‚źB[–óŽŅ’GAlpha‚Å‚ąPythonƒXƒNƒŠƒvƒg‚Ģģ¬‚šŽx‰‡‚·‚éPython-mode.tcl‚Ŗģ¬‚³‚ź‚Ä‚¢‚éB]

There is a BBEdit extension available that allows you to run Python scripts more-or-less straight from your bbedit source window. Check out the Mac:Tools:BBPy folder.
‚±‚ź‚©‚ē‚Ā‚­‚éƒXƒNƒŠƒvƒg‚Ģ–¼‘O‚ĢI‚ķ‚č‚š.py‚Ę‚µ‚Ä‚Ø‚­‚Ģ‚Ŗ‚ę‚¢‚¾‚ė‚¤B’P“Ę‚Å“®ģ‚·‚éƒXƒNƒŠƒvƒg‚É‚Ā‚¢‚Ä‚Ķ‚±‚ź‚Ķ•K‚ø‚µ‚ą•K—v‚Å‚Ķ‚Č‚¢‚ŖAƒ‚ƒWƒ…[ƒ‹‚Ģ–¼‘O‚Ķ‚±‚Ģƒ‹[ƒ‹‚ɏ]‚Į‚Ä‚Ø‚­Ž–‚Ŗ•K—v‚Å‚ ‚éB‚Å‚ ‚é‚©‚ē‚±‚ź‚šÅ‰‚©‚ēKŠµ‚Ę‚µ‚Ä‚Ø‚­‚±‚Ę‚Ķ‚Ø‚»‚ē‚­ˆÓ–”‚Ģ‚ ‚邱‚Ę‚¾‚ė‚¤B

It is a good idea to have the names of all your scripts end in .py. While this is not necessary for standalone scripts it is needed for modules, and it is probably a good idea to start the habit now.

‚ą‚µ‚ ‚Č‚½‚ŖPythonƒXƒNƒŠƒvƒg‚š‚Ā‚­‚Į‚Ä‚¢‚鎞‚É•ŅW|ŽĄs‚šŒJ‚č•Ō‚·‚½‚Ń‚ÉPythonƒCƒ“ƒ^ƒvƒŠƒ^‚š‹N“®‚·‚é‚Ģ‚šD‚Ü‚Č‚¢“Ž—ĒAimport•¶‚Ęreload()ŠÖ”‚šŽg‚Į‚ÄŠJ”­‚ĢƒXƒs[ƒhƒAƒbƒv‚š‚Ķ‚©‚邱‚Ę‚Ŗ‚Å‚«‚鏼‡‚ą‚ ‚éB‚±‚±‚ÉŽū‚ß‚é‚Ģ‚Ķ1.1”Å‚ĢƒŠƒŠ[ƒXƒm[ƒg‚©‚ēGuido‚Ģ‚ą‚Ę‚ą‚Ę‚ĢƒRƒƒ“ƒg‚Å‚ ‚éB

If you do not like to start the Python interpreter afresh for each edit-run cycle you can use the import statement and reload() function to speed things up in some cases. Here is Guido's original comment for how to do this, from the 1.1 release notes:

ƒvƒƒOƒ‰ƒ€‚Ķƒ‚ƒWƒ…[ƒ‹ƒtƒ@ƒCƒ‹(ƒtƒ@ƒCƒ‹–¼‚ĶPython‚ĢŽÆ•ŹŽq+'.py'‚Å‚ ‚邱‚Ę)‚Å‚ ‚邱‚ĘŠm”F‚µ‚ꂤB‚»‚¤‚·‚ź‚Ī‚»‚ĢƒvƒƒOƒ‰ƒ€‚šÅ‰‚ÉŽŽŒ±‚·‚邳‚¢‚É‚»‚ź‚šƒCƒ“ƒ|[ƒg‚·‚邱‚Ę‚Ŗo—ˆ‚éB‚±‚±‚ÅŽO‚Ā‚Ģ‰Ā”\«‚Ŗ‚ ‚éBƒC)•¶–@ƒGƒ‰[‚Ŗ‚ ‚éBƒ)ŽĄsŽžƒGƒ‰[(–¢ˆ——įŠO)‚Ę‚Č‚éBƒn)‚ ‚é‚¢‚ĶŽĄs‚Ķ‚·‚é‚ą‚Ģ‚Ģ•s³‚ČŒ‹‰Ź‚š•Ō‚·(‚ą‚µ“¾‚ē‚ź‚½Œ‹‰Ź‚Ŗ³‚µ‚Æ‚ź‚ĪA‚ ‚Č‚½‚ĢƒeƒXƒg‚ĶI‚ķ‚č‚Å‚ ‚éA‚±‚Ģƒpƒ‰ƒOƒ‰ƒt‚ĢŽc‚č‚š“Ē‚Ž•K—v‚Ķ‚Č‚¢)B‚±‚ĢŒć‚Ģ•”•Ŗ‚ĶMac“Į—L‚ĢŽ–‚Å‚Ķ‚Č‚¢‚±‚Ę‚É’ˆÓ‚µ‚Ä‚Ø‚­BUnix‚Å‚ĶƒXƒNƒŠƒvƒg‘S‘Ģ‚šŠČ’P‚ÉŽĄs‚·‚邱‚Ę‚Ŗo—ˆ‚邽‚߂ɁA‚ ‚Ü‚č—L—p‚Ę‚ĶŒ¾‚¦‚Č‚¢‚¾‚Æ‚Å‚ ‚éB

Make sure the program is a module file (filename must be a Python identifier followed by '.py'). You can then import it when you test it for the first time. There are now three possibilities: it contains a syntax error; it gets a runtime error (unhandled exception); or it runs OK but gives wrong results. (If it gives correct results, you are done testing and don't need to read the rest of this paragraph. :-) Note that the following is not Mac-specific -- it's just that on UNIX it's easier to restart the entire script so it's rarely useful.

•¶–@ƒGƒ‰[‚©‚ē‰ń•œ‚·‚é‚Ģ‚ĶŠČ’P‚Å‚ ‚éBƒtƒ@ƒCƒ‹‚š•ŅW‚µAÄ‚Ńimport‚·‚ź‚Ī‚ę‚¢B

Recovery from a syntax error is easy: edit the file and import it again.

•s³‚ȏo—Ķ‚©‚ē‰ń•œ‚·‚邱‚Ę‚ą“Æ‚¶ˆŹŠČ’P‚Å‚ ‚éBƒtƒ@ƒCƒ‹‚š•ŅW‚µAimport‚Ģ‚©‚ķ‚č‚Éreload()ŠÖ”‚šƒ‚ƒWƒ…[ƒ‹–¼‚šˆų”‚Ę‚µ‚ÄŒÄ‚Ńo‚·B(ƒ‚ƒWƒ…[ƒ‹–¼‚Ŗfoo ‚¾‚Ę‚·‚ź‚ĪAreload(foo)‚Ę“ü—Ķ‚·‚éB)

Recovery from wrong output is almost as easy: edit the file and, instead of importing it, call the function reload() with the module name as argument (e.g., if your module is called foo, type reload(foo)).

—įŠO‚©‚ē‚Ģ•œ‹A‚Ķ‚ā‚āƒgƒŠƒbƒL[‚Å‚ ‚éB•¶–@‚Ŗ³‚µ‚Æ‚ź‚ĪA"module"‚Ķ“ą•”‚Ģƒe[ƒuƒ‹‚É“o˜^‚³‚ź‚éAƒ‚ƒWƒ…[ƒ‹‚Ģ‰Šś‰»‚ŖƒGƒ‰[‚Å’†’f‚³‚ź‚½‚Ę‚µ‚Ä‚ą‚»‚ĢŒć‚Ģimport•¶‚ĢŽĄs‚Ķƒtƒ@ƒCƒ‹‚šÄ“x“Ē‚Żž‚Ž‚±‚Ę‚Ķ–³‚¢Bi‘ŠŒŻ‚ɍċA“I‚ÉŒÄ‚Ńo‚µ‚Ä‚¢‚郂ƒWƒ…[ƒ‹‚Ģ‰Šś‰»‚šˆź“x‚¾‚Æ‚Ę‚·‚鎖‚Ŗ‚±‚ĢŽd—l‚Ģ–Ś“I‚Ģˆź‚Ā‚Å‚ ‚éBj]‚Į‚āAreload()ŠÖ”‚šŽg‚Į‚ătƒ@ƒCƒ‹‚š‹­§“I‚ɍēĒž‚Ż‚³‚¹‚é•K—v‚Ŗ‚ ‚éB‚µ‚©‚µ‚Č‚Ŗ‚ēA‚ą‚µƒGƒ‰[‚Ŗ‰‚߂ă‚ƒWƒ…[ƒ‹‚šimport‚µ‚ꂤ‚Ę‚·‚é‚Ę‚«‚É‹N‚«‚½‚Č‚ēAimport•¶Ž©g‚ŖI—¹‚µ‚Ä‚¢‚Č‚¢A]‚Į‚ÄPythonƒCƒ“ƒ^ƒvƒŠƒ^‚Ķƒ‚ƒWƒ…[ƒ‹–¼‚š’m‚ē‚Č‚¢iƒ‚ƒWƒ…[ƒ‹‚šŠĒ—‚·‚éƒe[ƒuƒ‹‚Ķ‚»‚ź‚š’m‚Į‚Ä‚¢‚½‚Ę‚µ‚Ä‚ąIjB‚±‚Ģó‹µ‚šŲ‚č”²‚Æ‚éƒgƒŠƒbƒN‚Ę‚µ‚āA‚Ü‚øimport•¶‚šÄ“xŽĄs‚µA‚»‚ĢŒćreload()‚šŒÄ‚яo‚·B—į‚¦‚ĪA
import foo; reload(foo); ‚Ģ‚ꂤ‚É‚Å‚ ‚éBŠł‚Ƀ‚ƒWƒ…[ƒ‹ƒIƒuƒWƒFƒNƒg‚Ķ“ą•”“I‚É‚Ķ‘¶Ż‚·‚邽‚ß‚É‚±‚Ģƒ‚ƒWƒ…[ƒ‹‚Ģ‰Šś‰»‚šÄ“xŽĄs‚µ‚ꂤ‚Ę‚Ķ‚µ‚Č‚¢B’P‚ɍģ‹Ę—Ģˆę‚Ƀ‚ƒWƒ…[ƒ‹–¼‚š’u‚­‚¾‚Æ‚Å‚ ‚éB
 

Recovery from an exception is trickier. Once the syntax is correct, a 'module' entry is placed in an internal table, and following import statements will not re-read the file, even if the module's initialization terminated with an error (one reason why this is done is so that mutually recursive modules are initialized only once). You must therefore force re-reading the module with reload(), however, if this happens the first time you try to import the module, the import statement itself has not completed, and your workspace does not know the module name (even though the internal table of moduesl does!). The trick is to first import the module again, then reload it. For instance, import foo; reload(foo). Because the module object already exists internally, the import statement does not attempt to execute the module again -- it just places it in your workspace.

ƒNƒŠƒbƒN‰Ā”\‚ČPythonƒXƒNƒŠƒvƒg-- Clickable python scripts --


‚ ‚Č‚½‚ĢƒXƒNƒŠƒvƒg‚Ŗ³‚µ‚¢ƒtƒ@ƒCƒ‹EƒNƒŠƒG[ƒ^‚ĘŒ^( 'Pyth'ƒNƒŠƒG[ƒ^‚Ę'TEXT'ƒ^ƒCƒv)‚šŽ‚Į‚Ä‚¢‚é‚Č‚ēA‚±‚ĢƒXƒNƒŠƒvƒgƒtƒ@ƒCƒ‹‚šƒ_ƒuƒ‹ƒNƒŠƒbƒN‚·‚邱‚Ę‚ÅAPython Interpreter‚Ŗ‹N“®‚³‚ź‚±‚ĢƒXƒNƒŠƒvƒg‚ŖŽĄs‚³‚ź‚éB‚ą‚µBBEdit‚šŽg‚Į‚Ä‚¢‚é‚Č‚ē‰ŠśŻ’č‚Ģ"file type"ƒZƒNƒVƒ‡ƒ“‚ÉPython‚Ģƒtƒ@ƒCƒ‹ƒ^ƒCƒv‚š•t‚ƉĮ‚¦‚邱‚Ę‚Ŗo—ˆ‚éB‚»‚ĢŒćƒXƒNƒŠƒvƒgƒtƒ@ƒCƒ‹‚šÅ‰‚É•Ū‘¶‚·‚é‚Ę‚«‚É•Ū‘¶ƒ_ƒCƒAƒƒO‚Ģ"options"‚š‘I‘š‚·‚邱‚Ę‚ÅBBEdit‚ÉPythonƒXƒNƒŠƒvƒg‚Ę‚µ‚Ä•Ū‘¶‚·‚邱‚Ę‚š‚µ‚Ä‚¢‚·‚邱‚Ę‚Ŗo—ˆ‚éB
i–óŽŅ’FAlphaƒGƒfƒBƒ^‚Å‚ĶA"Utils -> File Utils -> File Info"ƒƒjƒ…[‚šŽg‚Į‚ătƒ@ƒCƒ‹ƒ^ƒCƒv‚šŻ’č‚·‚邱‚Ę‚Ŗo—ˆ‚éBj

If you create your script with the correct creator and type, creator 'Pyth' and type 'TEXT', you can double-click your script and it will automatically invoke the interpreter. If you use BBEdit you can tell it about the Python file type by adding it to the "file types" sections of the preferences. Then, if you save a file for the first time you can tell BBEdit to save the file as a Python script through the "options" choice of the save dialog.

"Scripts"ƒtƒHƒ‹ƒ_‚Ķ"fixfiletypes"ƒXƒNƒŠƒvƒg‚šŠÜ‚ń‚Å‚¢‚éB‚±‚ĢƒXƒNƒŠƒvƒg‚ĶÄ‹A“I‚ɃtƒHƒ‹ƒ_‚Ģ’†‚š’Tø‚µA".py"‚ŏI‚ķ‚éƒtƒ@ƒCƒ‹–¼‚š‚ą‚Āƒtƒ@ƒCƒ‹‚Ģƒtƒ@ƒCƒ‹ƒ^ƒCƒv‚šPython‚Ģƒtƒ@ƒCƒ‹ƒ^ƒCƒv‚É•ĻX‚·‚éB

The Scripts folder contains a script fixfiletypes that will recursively traverse a folder and set the correct creator and type for all files ending in .py.

ŒĆ‚¢Python ƒCƒ“ƒ^ƒvƒŠƒ^‚Ķ'PYTH'‚šƒNƒŠƒG[ƒ^ƒR[ƒh‚Ę‚µ‚ÄŽ‚Į‚Ä‚¢‚éB‚ą‚µŒĆ‚¢PythonƒXƒNƒŠƒvƒg‚Ģƒ\[ƒX‚š‚ą‚Į‚Ä‚Ø‚čA‚»‚ź‚ē‚Ģƒtƒ@ƒCƒ‹–¼‚Ŗ".py"‚Å‚Ø‚ķ‚Į‚Ä‚¢‚é‚Č‚ē‚±‚ĢfixfiletypesƒXƒNƒŠƒvƒg‚ĶV‚µ‚¢ƒNƒŠƒG[ƒ^ƒ^ƒCƒv"Pyth"‚É•ĻX‚µ‚Ä‚­‚ź‚éB

Older releases of Python used the creator code 'PYTH' in stead of 'Pyth'. If you still have older Python sources on your system and named them with '.py' extension the fixfiletypes script will correct them.

ƒ†[ƒU‚Ę‚Ģ‘ŠŒŻģ—p -- Interaction with the user --

’ŹķAƒCƒ“ƒ^ƒvƒŠƒ^‚Ķƒ†[ƒU‚©‚ē‚Ģ“ü—Ķ(ƒ}ƒEƒX‚ĢƒNƒŠƒbƒNAƒL[ƒ{[ƒh“ü—Ķ‚Č‚Ē)‚š’芜“I‚Ƀ`ƒFƒbƒN‚µ‚Ä‚¢‚éB]‚Į‚āAƒXƒNƒŠƒvƒg‚šŽĄs’†‚É‘¼‚ĢƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚ɐ§Œä‚š‚ķ‚½‚·‚±‚Ę‚Ŗ‰Ā”\‚Å‚ ‚éBŽĄs’†‚ĢƒXƒNƒŠƒvƒg‚š•W€“I‚ČƒRƒ}ƒ“ƒh-ƒsƒŠƒIƒh'.'‚ĢƒL[‘g‚Ż‡‚ķ‚¹‚ÅŽĄs‚š’†Ž~‚·‚邱‚Ę‚Ŗo—ˆ‚éB‚±‚ĢƒL[“ü—Ķ‚Ķ"KeyboardInterrupt"—įŠO‚š”­¶‚·‚éBƒXƒNƒŠƒvƒg‚ĶA‚»‚źŽ©‘Ģ‚ĢƒGƒxƒ“ƒgˆ—‚šŽĄs‚·‚邽‚ß‚É‚±‚ĢU‚é•‘‚¢‚š•s‰Ā‚Ę‚·‚邱‚Ę‚ąo—ˆ‚éB‚»‚Ģ‚ꂤ‚ČƒXƒNƒŠƒvƒg‚Ķcommand-option-escapeƒVƒ‡[ƒgƒJƒbƒg‚Å‚Ģ‚ŻŽĄs‚š’†Ž~‚·‚邱‚Ę‚Ŗ‰Ā”\‚Å‚ ‚éB
Normally, the interpreter will check for user input (mouse clicks, keyboard input) every once in a while, so it is possible to switch to other applications while a script runs. It is also possible to interrupt the interpreter with the standard command-period keypress, this will raise the KeyboardInterrupt exception. Scripts may, however, turn off this behaviour to facilitate their own event handling. Such scripts can only be killed with the command-option-escape shortcut.

‹N“®ŽžƒIƒvƒVƒ‡ƒ“ -- startup options --

Python ‚š‹N“®‚·‚éŪ‚ÉoptionƒL[‚Ŗ‰Ÿ‚³‚ź‚Ä‚¢‚é‚ʁA‹N“®ŽžƒIƒvƒVƒ‡ƒ“‚šŻ’č‚·‚邽‚ß‚Ģƒ_ƒCƒAƒƒO‚Ŗ•\Ž¦‚³‚ź‚éB‚±‚Ģƒ_ƒCƒAƒƒO‚š’Ź‚¶‚ăCƒ“ƒ^ƒvƒŠƒ^‚ĢU‚é•‘‚¢‚š•ĻX‚·‚邱‚Ę‚Ŗ‰Ā”\‚Ę‚Č‚éBƒ_ƒCƒAƒƒO‚Ŗ•\Ž¦‚³‚ź‚é‚܂ŁAoptionƒL[‚š‰Ÿ‚µ‘±‚Ƃ鎖B

If the option key is depressed when Python starts executing the interpreter will bring up an options dialog thru which you can influence the way the interpreter behaves. Keep the option key depressed until the dialog comes up.

 ƒIƒvƒVƒ‡ƒ“‚ĶŽŸ‚Ģ‚ꂤ‚Č•ū–@‚ŃCƒ“ƒ^ƒvƒŠƒ^‚ĢU‚é•‘‚¢‚š•ĻX‚·‚éB

The options modify the interpreters behaviour in the following way:

‚³‚ē‚ɁAUnix Ž®‚ĢƒRƒ}ƒ“ƒhƒ‰ƒCƒ“‚šŽg‚Į‚āAƒXƒNƒŠƒvƒg‚Ģsys.argv ‚É’l‚š“n‚·‚±‚Ę‚Ŗ‰Ā”\‚Å‚ ‚éBsys.argv[0]‚Ķķ‚ÉŽĄs‚³‚ź‚Ä‚¢‚é‚·‚é‚­ƒv‚Ę‚Ģ–¼‘O‚Å‚ ‚éB’Ē‰Į‚Ģī•ń‚ĶŸˆ‚ŃXƒNƒŠƒvƒg‚É“n‚·‚±‚Ę‚Ŗo—ˆ‚éB•¶Žš—ń‚Ģˆų—p‚Ķ—\Šś‚µ‚½’Ź‚č‚É“®ģ‚·‚éB
In addition, you can enter a unix-style command line which is passed to the script in sys.argv. Sys.argv[0] is always the name of the script being executed, additional values can be passed here. Quoting works as expected.
ŒxGƒRƒ}ƒ“ƒhƒ‰ƒCƒ“ƒ_ƒCƒAƒƒO‚Ģ•W€“ü—Ķ‚Ę•W€o—Ķ‚ĢŠ„“–‚Ģ•ĻX‚Ķ“®ģ‚µ‚Č‚¢B‚±‚ź‚ĶŽ„‚ĢŽč‚Ģ“Ķ‚­”ĶˆĶ‚ĢŠO‚ÉŒ“ˆö‚Ŗ‚ ‚邽‚߁A‚±‚ź‚Ŗ‰šŒˆ‚³‚ź‚é‚©‚Ē‚¤‚©‚šŒ¾‚¤‚±‚Ę‚Ķo—ˆ‚Č‚¢B
Warning: redirecting standard input or standard output in the command-line dialog does not work. This is due to circumstances beyond my control, hence I cannot say when this will be fixed.
ƒIƒvƒVƒ‡ƒ“‚ĢŠł’č’l‚ĶƒVƒXƒeƒ€‘S‘Ģ‚Å‚ąŻ’č‰Ā”\‚Å‚ ‚éBu‰ŠśŻ’č‚Ģ•ŅWv‚Ģß‚šŒ©‚Ä—~‚µ‚¢B
The default options are also settable on a system-wide basis, see the section on editing preferences.

ƒ‚ƒWƒ…[ƒ‹‚ĢŒŸõźŠ -- Module search path --

ƒ‚ƒWƒ…[ƒ‹ŒŸõƒpƒX, sys.path, ‚Ķƒ‚ƒWƒ…[ƒ‹‚šimport‚·‚éŪ‚ÉPython‚Ŗƒ‚ƒWƒ…[ƒ‹‚š’T‚·ƒtƒHƒ‹ƒ_‚Ģ–¼‘O‚šŽ‚Į‚Ä‚¢‚éB‚±‚ĢƒpƒX‚ĶƒVƒXƒeƒ€‘S‘Ģ‚ĢŠł’č’l‚Ę‚µ‚ĐŻ’č‚·‚邱‚Ę‚Ŗ‰Ā”\‚Å‚ ‚é(‰ŠśŻ’č‚Ģß‚šŽQĘ)B‚Ü‚½’Źķ‚ĶƒXƒNƒŠƒvƒgƒtƒ@ƒCƒ‹‚Ģ‘¶Ż‚·‚錻Ż‚ĢƒtƒHƒ‹ƒ_ALibƒtƒHƒ‹ƒ_‚Ø‚ę‚Ń‚»‚ĢƒTƒuƒtƒHƒ‹ƒ_A‚»‚µ‚Ä‚»‚Ģ‘¼‚ĢƒtƒHƒ‹ƒ_‚Ŗsys.path‚ÉŠÜ‚Ü‚ź‚Ä‚¢‚éB

The module search path, sys.path, contains the folders python will search when you import a module. The path is settable on a system-wide basis (see the preferences section), and normally comprises the current folder (where the script lives), the Lib folder and some of its subfolders and possibly some more.

ģ‹ĘƒtƒHƒ‹ƒ_ -- Working folder --

Unix‚ĢŠT”O‚Å‚ ‚éģ‹ĘƒfƒBƒŒƒNƒgƒŠ‚šMacintoshć‚Å’¼Ś‘Ī‰ž‚·‚é‚ą‚Ģ‚Ķ–³‚¢BˆŚA‚Ę‘Š‘Ī‚Ļ‚·–¼‚ĢŽd—l‚šŠČ’P‚É‚·‚邽‚߂ɁAƒCƒ“ƒ^ƒvƒŠƒ^‚Ķģ‹ĘƒfƒBƒŒƒNƒgƒŠ‚šƒVƒ~ƒ…ƒŒ[ƒg‚µ‚Ä‚¢‚éBƒXƒNƒŠƒvƒg‚Ŗ‹N“®‚³‚ź‚½‚Ę‚«A‰Šś‚Ģģ‹ĘƒfƒBƒŒƒNƒgƒŠ‚ĶƒXƒNƒŠƒvƒg‚Ģƒtƒ@ƒCƒ‹‚Ŗ‚ ‚éƒtƒHƒ‹ƒ_‚Å‚ ‚éB‘Ī˜bƒ‚[ƒh‚Å‹N“®‚³‚ź‚½ƒCƒ“ƒ^ƒvƒŠƒ^‚Ģģ‹ĘƒfƒBƒŒƒNƒgƒŠ‚ĶƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ‚ ‚éƒtƒHƒ‹ƒ_‚ŃXB

The unix concept of a working directory does not translate directly to a similar concept on the Macintosh. To facilitate easy porting and the use of relative pathnames in scripts the interpreter simulates a working directory. When a script is started the initial working directory is the folder where the script lives. In case of an interactive interpreter the working directory is the folder where the interpreter lives.

‚Ę‚±‚ė‚ŁAƒI[ƒvƒ“‚ ‚é‚¢‚Ķ•Ū‘¶ƒ_ƒCƒAƒƒO‚ц[ƒU‚ɍŏ‰‚ÉŽ¦‚³‚ź‚éƒtƒHƒ‹ƒ_‚Å‚ ‚é"•W€ƒtƒ@ƒCƒ‹ƒtƒHƒ‹ƒ_"‚ĶPython‚Ģģ‹ĘƒfƒBƒŒƒNƒgƒŠ‚Ę˜A“®‚µ‚Ä‚¢‚Č‚¢Bƒ†[ƒU‚É‚±‚ź‚ē‚Ģƒ_ƒCƒAƒƒO‚Å’ńŽ¦‚³‚ź‚éƒtƒHƒ‹ƒ_‚Ķ@(a)ƒAƒvƒŠƒP[ƒVƒ‡ƒ“ƒtƒHƒ‹ƒ_A(b) "Documents"ƒtƒHƒ‹ƒ_A(c) ‚»‚ź‚ē‚Ģƒ_ƒCƒAƒƒO‚ōŌć‚ÉŽg‚ķ‚ź‚½ƒtƒHƒ‹ƒ_‚Ģ“ą‚Ģˆź‚Ā‚Å‚ ‚éB‚±‚ź‚ĶMacOS‚Ģ•W€“I‚ȐU‚é•‘‚¢‚Å‚ ‚éA‚¾‚©‚ēPython‚É‚»‚ĢÓ”C‚š–ā‚ķ‚Č‚¢‚Å—~‚µ‚¢BSystem7.5ˆČ~‚ĢMacOS‚Å‚ĶƒRƒ“ƒgƒ[ƒ‹ƒpƒlƒ‹‚Å‚±‚ź‚ē‚ĢU‚é•‘‚¢‚šŻ’č‚·‚邱‚Ę‚Ŗo—ˆ‚éB
 

By the way: the "standard file" folder, the folder that is presented to the user initially for an open or save dialog, does not follow the Python working directory. Which folder is initially shown to the user is usually one of (a) the application folder, (b) the "Documents" folder or (c) the folder most recently used for such a dialog (in any Python program). This is standard MacOS behaviour, so don't blame Python for it. The exact behaviour is settable through a control panel since System 7.5.

‘Ī˜b“IƒXƒ^[ƒgƒAƒbƒvƒtƒ@ƒCƒ‹ -- Interactive startup file --

‚ą‚µPython ƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ‘¶Ż‚·‚éƒtƒHƒ‹ƒ_’†‚É"PythonStartup"‚ĘŒ¾‚¤–¼‘O‚Ģƒtƒ@ƒCƒ‹‚Ŗ‚ ‚é‚ʁA‚±‚Ģƒtƒ@ƒCƒ‹‚Ķ‘Ī˜bƒ‚[ƒh‚ŃCƒ“ƒ^ƒvƒŠƒ^‚š‹N“®‚µ‚½Ū‚ÉŽĄs‚³‚ź‚éB‚±‚Ģƒtƒ@ƒCƒ‹‚Ģ’†‚ŁA‚ę‚­Žg‚¤ƒ‚ƒWƒ…[ƒ‹‚šimport‚·‚é‚Č‚Ē‚šs‚Č‚¦‚éB

If the folder containing the interpreter contains a file named PythonStartup this file is executed when you start an interactive interpreter. In this file you could import modules you often use and other such things.

ƒRƒ“ƒpƒCƒ‹‚³‚ź‚½PythonƒXƒNƒŠƒvƒg -- Compiled python scripts --

ˆź’[Pythonƒ‚ƒWƒ…[ƒ‹‚Ŗimport‚³‚ź‚é‚ʁAƒCƒ“ƒ^ƒvƒŠƒ^‚Ķ‚»‚ĢƒXƒNƒŠƒvƒg‚ĢƒRƒ“ƒpƒCƒ‹‚³‚ź‚½ƒo[ƒWƒ‡ƒ“‚š'.py'‚š'.pyc'‚Å’u‚«Š·‚¦‚½–¼‘O‚Ģƒtƒ@ƒCƒ‹‚Ę‚µ‚Ä•Ū‘¶‚·‚éB‚±‚ź‚ē‚ĢƒRƒ“ƒpƒCƒ‹‚³‚ź‚½ƒtƒ@ƒCƒ‹iƒtƒ@ƒCƒ‹ƒNƒŠƒG[ƒ^ 'Pyth',  ƒtƒ@ƒCƒ‹ƒ^ƒCƒv'PYC'‚š‚ą‚Āj‚Ķimport‚³‚ź‚邳‚¢‚ɍ‚‘¬‚Ƀ[ƒh‚·‚邱‚Ę‚Ŗo—ˆ‚é(‚±‚ź‚ē‚Ģƒtƒ@ƒCƒ‹‚Ķ•¶–@‰šĶ‚³‚ź‚é•K—v‚Ŗ‚Č‚¢‚©‚ē)BLibƒtƒHƒ‹ƒ_‚É‚Ķcompileall.pyƒXƒNƒŠƒvƒg‚ŖŠÜ‚Ü‚ź‚Ä‚¢‚éB‚±‚ĢƒXƒNƒŠƒvƒg‚šŽĄs‚·‚é‚ĘPython‚ĢŒŸõƒpƒX‚ÉŠÜ‚Ü‚ź‚é‚·‚ׂĂĢƒ‚ƒWƒ…[ƒ‹‚š‚ ‚ē‚©‚¶‚߃Rƒ“ƒpƒCƒ‹‚µ‚Ä‚Ø‚­‚±‚Ę‚Ŗo—ˆ‚éB‚±‚ź‚É‚ę‚Į‚Ä‚¢Python‚Ģ‹N“®‚š‚‘¬‰»‚·‚邱‚Ę‚Ŗo—ˆ‚éBƒRƒ“ƒpƒCƒ‹‚³‚ź‚½ƒtƒ@ƒCƒ‹‚ąƒ_ƒuƒ‹EƒNƒŠƒbƒN‚ÅŽĄsŠJŽn‰Ā”\‚Å‚ ‚éB

Once a python module has been imported the interpreter creates a compiled version which is stored in a file with the ".py" extension replaced by ".pyc". These compiled files, with creator 'Pyth' and type 'PYC ' load faster when imported (because they do not have to be parsed). The Lib folder contains a script compileall.py, running this script will cause all modules along the python search path to be precompiled, which will speed up your programs. Compiled files are also double-clickable.

Python‚ĢƒŠƒ\[ƒX -- Python resources --

MacPython‚É‚Ķˆź‚Ā‚Ģƒtƒ@ƒCƒ‹‚ĢƒŠƒ\[ƒXƒtƒH[ƒN‚ÉŠÜ‚Ü‚ź‚é•””‚ĢƒRƒ“ƒpƒCƒ‹‚³‚ź‚½ƒ‚ƒWƒ…[ƒ‹‚šW‚ß‚é‹@”\‚Ŗ‚ ‚éB‚±‚Ģ‹@”\‚Ķ‚ą‚µPythonƒvƒƒOƒ‰ƒ€‚š”z•t‚µA‚²‚½‚²‚½‚šÅ¬‚É‚µ‚½‚¢Žž‚É–š—§‚ĀB‚·‚ׂĂĢ•K—v‚Čƒ‚ƒWƒ…[ƒ‹(‚»‚ź‚ĶƒCƒ“ƒ^ƒvƒŠƒ^‚»‚Ģ•Ø‚Å‚ ‚Į‚Ä‚ą‚ę‚¢)‚šˆź‚Ā‚Ģƒtƒ@ƒCƒ‹‚ÉŽū‚߂邱‚Ę‚ą‰Ā”\‚Å‚ ‚éB

MacPython has the ability to collect a number of compiled modules together in the resource fork of a single file. This feature is useful if you distribute a python program and want to minimize clutter: you can put all the needed modules in a single file (which could even be the interpreter itself).

‚ą‚µƒ‚ƒWƒ…[ƒ‹ŒŸõƒpƒX‚Ŗƒtƒ@ƒCƒ‹–¼i’Źķ‚Ķ‚±‚ź‚ĶƒtƒHƒ‹ƒ_–¼‚Å‚ ‚éj‚šŠÜ‚ń‚Å‚¢‚é‚Ę‚±‚Ģƒtƒ@ƒCƒ‹‚ĢƒŠƒ\[ƒXƒtƒH[ƒN‚Ģ'PYC'ƒŠƒ\[ƒX‚©‚ēimport‚³‚ź‚郂ƒWƒ…[ƒ‹‚šŒŸõ‚³‚ź‚éB
If the module search path contains a filename as one of its entries (as opposed to a folder name, which is the normal case) this file will be searched for a resource with type 'PYC ' and a name matching the module being imported.

ƒXƒNƒŠƒvƒgƒtƒHƒ‹ƒ_‚ĶPackLibDirƒXƒNƒŠƒvƒg‚šŠÜ‚ń‚Å‚¢‚éB‚±‚ĢƒXƒNƒŠƒvƒg‚Ķ•””‚Ģƒ‚ƒWƒ…[ƒ‹‚š‚±‚Ģ‚ꂤ‚ČƒŠƒ\[ƒXƒtƒ@ƒCƒ‹‚É•ĻŠ·‚·‚éB

The scripts folder contains a script PackLibDir which will convert a number of modules (or possibly a complete subtree full of modules) into such a resource file.

ƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ‰ŠśŻ’č

Setting interpreter preferences

PythonƒCƒ“ƒ^ƒvƒŠƒ^‚Ķ‰ŠśŻ’čƒtƒ@ƒCƒ‹‚šƒVƒXƒeƒ€ƒtƒHƒ‹ƒ_’†‚Ģ•W€“I‚ȏźŠi‰ŠśŻ’čƒtƒHƒ‹ƒ_j‚É’u‚­B‚±‚Ģ‰ŠśŻ’čƒtƒ@ƒCƒ‹‚É’†‚É‚ĶAƒfƒtƒHƒ‹ƒg‚Ģƒ‚ƒWƒ…[ƒ‹ŒŸõ‚Ģ“¹‹Ų‚ā‹N“®ŽžƒIƒvƒVƒ‡ƒ“‚ĢŠł’č’l‚Č‚Ē‚ŖŽū‚ß‚ē‚ź‚Ä‚¢‚éB‰ŠśŻ’č‚ĶEditPythonPrefsƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚š’Ź‚¶‚Ä•ĻX‚Ŗ‰Ā”\‚Å‚ ‚éBPPC/cmf68k Python ‚Å‚Ķ‚±‚ĢEditPythonPrefsƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ķ’P“Ę‚Å‹N“®‰Ā”\‚ČƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ę‚µ‚ÄPython‚ĢƒtƒHƒ‹ƒ_‚É’u‚©‚ź‚Ä‚¢‚éB68K—p‚ĢPython‚Å‚ĶEditPythonPrefs‚ĶMacScriptsƒtƒHƒ‹ƒ_‚É’u‚©‚ź‚½PythonƒXƒNƒŠƒvƒg‚Å‚ ‚éB

The python interpreter keeps a preferences file in the standard location in the system folder. In this preferences file it remembers the default module search path and the default settings for the runtime options. The preferences are settable via EditPythonPrefs. For PPC/cfm68k python this is a standalone program living in the main Python folder, for 68K python it is a script in the Mac:Scripts folder.

Œ»Ż‚Ģ”Å‚Ģ‰ŠśŻ’č‚š•ŅW‚·‚éƒCƒ“ƒ^ƒtƒF[ƒX‚ĶŽįŠ±Clunky‚Å‚ ‚éB
The interface to edit the preferences is rather clunky for the current release.

 Åć•”‚Ģ•ŅW‰Ā”\‚ČƒeƒLƒXƒgƒtƒB[ƒ‹ƒh‚Ƀ‚ƒWƒ…[ƒ‹‚šŒŸõ‚·‚éƒpƒX‚Ģ‰Šś’l‚š“ü—Ķ‚·‚éBƒ‚ƒWƒ…[ƒ‹ƒpƒX‚Ķ‰üs‚Å‹ęŲ‚ē‚ź‚éB‚±‚±‚ÅŽg‚¤‚±‚Ę‚Ģo—ˆ‚é“Į•Ź‚Č’l‚Ŗ“ń‚Ā‚ ‚éBƒ}ƒNƒ$(PYTHON)‚ĶPython‚Ģƒz[ƒ€ƒtƒHƒ‹ƒ_‚Ģ–¼‘O‚É“WŠJ‚³‚ź‚éB$(APPLICATION) ‚ĶƒpƒCƒ\ƒ“ƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚»‚Ģ‚ą‚Ģ‚É“WŠJ‚³‚ź‚éBƒeƒLƒXƒgƒtƒB[ƒ‹ƒh‚É‚ĶƒXƒNƒ[ƒ‹ƒo[‚Ŗ–³‚¢‚Æ‚ź‚Ē‚ąAƒtƒB[ƒ‹ƒh‚ĢÅ‰ŗ•”‚š‰z‚¦‚Ä“ü—Ķ‰Ā”\‚Å‚ ‚邱‚Ę‚É’ˆÓ‚µ‚Ä‚Ø‚­BƒJ[ƒ\ƒ‹ƒL[‚ňŚ“®‚·‚邱‚Ę‚Ŗo—ˆ‚éB

In the editable text field at the top you enter the initial module search path, using newline as a separator. There are two special values you can use here: an initial substring $(PYTHON) will expand to the Python home folder and a value of $(APPLICATION) will expand to the the python application itself. Note that the text field may extend "beyond the bottom" even though it does not have a scroll bar. Using the arrow keys works, though.

Python ƒz[ƒ€ƒtƒHƒ‹ƒ_$(PYTHON)‚Ķ‰Šśó‘Ō‚·‚Č‚ķ‚æ‚ ‚Č‚½‚ŖPython‚šƒCƒ“ƒXƒg[ƒ‹‚µ‚½Žž‚ɁAƒCƒ“ƒ^ƒvƒŠƒ^‚Ģ‚ ‚éƒtƒHƒ‹ƒ_‚ɐŻ’č‚³‚ź‚éBŸˆ‚ŁA‚±‚ź‚š•ĻXo—ˆ‚éB

 The Python home folder $(PYTHON) is initially, when you install Python, set to the folder where the interpreter lives. You can change it here.

ÅŒć‚ɁA‹N“®ŽžƒIƒvƒVƒ‡ƒ“‚ĢŠł’č’l‚š•›ƒ_ƒCƒAƒƒO‚š’Ź‚¶‚Ä•ĻXo—ˆ‚éB
Finally, you can set the default startup options here, through a sub-dialog.

ƒAƒvƒŒƒbƒg@|Applets|

Python ƒAƒvƒŒƒbƒg‚Ķ,ƒAƒbƒvƒ‹ƒXƒNƒŠƒvƒg‚ŏ‘‚©‚ź‚½ƒAƒvƒŒƒbƒg‚Ę“Æ‚¶‚ꂤ‚ɁAPython‚Å‹Lq‚³‚ź‚½Š®‘S‚É‹@”\‚·‚éƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Å‚ ‚éB
(‚»‚µ‚āAJavaƒAƒvƒŒƒbƒg‚Ę‚ĶŠ®‘S‚ɈŁ‚Č‚Į‚Ä‚¢‚éB)@ƒAƒvƒŒƒbƒg‚ĶPowerPCƒ}ƒbƒLƒ“ƒgƒVƒ…‚ĘCFM68K”Å‚ĢƒCƒ“ƒ^ƒvƒŠƒ^‚š‚Ā‚©‚¤68Kƒ}ƒbƒLƒ“ƒgƒbƒVƒ…‚ŃTƒ|[ƒg‚³‚ź‚Ä‚¢‚éBƒAƒvƒŒƒbƒg‚ĶBuildAppletƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚ōģ¬‚³‚ź‚éBPythonƒ\[ƒXƒR[ƒh‚ĢŽū‚ß‚ē‚ź‚½ƒtƒ@ƒCƒ‹‚šBuildAppletƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ƀhƒƒbƒv‚·‚邾‚ƂŃAƒvƒŒƒbƒg‚Ŗģ¬‚³‚ź‚éB—į2‚ĶA‚»‚źŽ©g‚ĢƒŠƒ\[ƒX‚š‚ą‚Ā‚Č‚Ē‚ą‚¤­‚µž‚Ż“ü‚Į‚½ƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Å‚ ‚éB

An applet is a fullblown application written in Python, similar to an AppleScript applet (and completely different from a Java applet). Applets are currently supported on PowerPC macintoshes and on 68K macintoshes if you use the CFM68K version of the interpreter, and are created using the BuildApplet program. You create an applet by dropping the python source script onto BuildApplet. Example 2 is a more involved applet with its own resource file, etc.

ƒAƒvƒŒƒbƒg‚ĶŠ®‘S‚Čƒ}ƒbƒLƒ“ƒgƒVƒ…ƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ę‚µ‚ĐU‚é•‘‚¤‚Æ‚ź‚ĒA‚»‚źŽ©g‚¾‚Æ‚Å‚Ķ•Ā‚¶‚Ä‚¢‚Č‚¢‚±‚Ę‚É’ˆÓ‚µ‚Č‚Æ‚ź‚Ī‚Č‚ē‚Č‚¢B]‚Į‚āAPythonƒCƒ“ƒ^ƒvƒŠƒ^‚šƒCƒ“ƒXƒg[ƒ‹‚µ‚Ä‚¢‚Č‚¢ƒ}ƒbƒLƒ“ƒgƒbƒVƒ…‚ɃAƒvƒŒƒbƒg‚š”z•t‚µ‚Ä‚ą“®ģ‚µ‚Č‚¢BƒAƒvƒŒƒbƒg‚ĶPython‚ĢŽĄsƒGƒ“ƒWƒ“PythonCore‚š•K—v‚Ę‚µ‚Ä‚¢‚éA‚Ü‚½‚Ø‚»‚ē‚­Lib‚Ø‚ę‚ŃPlugInsƒtƒHƒ‹ƒ_’†‚Ģ‚³‚Ü‚“‚Ü‚Čƒ‚ƒWƒ…[ƒ‹‚š•K—v‚Ę‚·‚邾‚ė‚¤B‚±‚ź‚ē‚ŖƒCƒ“ƒXƒg[ƒ‹‚³‚ź‚Ä‚¢‚Č‚¢‹@ŠB‚ɃAƒvƒŒƒbƒg‚š”z•t‚µ‚½‚Ä‚ą‚»‚ź‚Ķ“®ģ‚µ‚Č‚¢B

Note that while an applet behaves as a fullblown Macintosh application it is not self-sufficient, so distributing it to a machine without an installed Python interpreter will not work: it needs the shared python execution engine PythonCore, and probably various modules from the Lib and PlugIns folders. Distributing it to a machine that does have a Python system will work.

ƒAƒvƒŒƒbƒg‚ĢƒJƒXƒ^ƒ}ƒCƒYCustomizing applets

ƒAƒvƒŒƒbƒg‚Ķ‚»‚źŽ©g‚Ģ‹N“®ŽžƒIƒvƒVƒ‡ƒ“‚ĢŻ’č‚Ęƒ‚ƒWƒ…[ƒ‹ŒŸõƒpƒX‚ĢŻ’č‚šŽ‚ĀBƒAƒvƒŒƒbƒg‚šEditPyhonPrefsƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ƀhƒƒbƒv‚·‚邱‚Ę‚ÅA‚±‚ź‚ē‚ĢŻ’čƒ_ƒCƒAƒƒO‚ĶŠJ‚©‚ź‚éBƒ_ƒCƒAƒƒOƒEƒBƒ“ƒhƒE‚ĶEditPythonPref‚šƒ_ƒuƒ‹EƒNƒŠƒbƒN‚µ‚ÄŠJ‚­ƒVƒXƒeƒ€ƒƒCƒh‚ȐŻ’č‚š•ŅW‚·‚éƒEƒBƒ“ƒhƒE‚Ę“Æ‚¶‚Å‚ ‚éB

Applets can have their own settings for the startup options and module search path. Dropping an applet on the EditPythonPrefs application allows you to set these, in the same way as double-clicking EditPythonPrefs allows you to set the system-wide defaults.

‚³‚ē‚ÉŒ¾‚¦‚ĪAƒAƒvƒŒƒbƒg‚¾‚Æ‚Å‚Č‚­ƒCƒ“ƒ^ƒvƒŠƒ^‚»‚źŽ©g‚ąŠł’č’l‚Ę‚ĶˆŁ‚Č‚éƒpƒX‚ĘƒIƒvƒVƒ‡ƒ“‚š‚ą‚Ā‚±‚Ę‚Ŗo—ˆ‚éB‚ą‚µAƒCƒ“ƒ^ƒvƒŠƒ^‚ĢƒRƒs[‚š‚Ā‚­‚č‚»‚ĢƒRƒs[‚šEditPythonPrefsƒAƒvƒŠƒP[ƒVƒ‡ƒ“‚Ƀhƒƒbƒv‚·‚邱‚Ę‚ÅŠł’č’l‚Ę‚ĶˆŁ‚Ȃ鐯’č‚šo—ˆ‚éB

Actually, not only applets but also the interpreter itself can have non-default settings for path and options. If you make a copy of the interpreter and drop this copy onto EditPythonPrefs you will have an interpreter that has a different set of default settings.

‚Ē‚±‚ɐi‚Ž‚ׂ«‚©HWhere to go from here

ę‚ɐG‚ź‚½Python Tutorial‚ĶAPython‚š‚±‚ź‚Ü‚Å‚Ā‚©‚Į‚½‚±‚Ę‚Ŗ–³‚¢‚Ģ‚Č‚ēA“Ē‚ŻŽn‚ß‚é‚Ģ‚ɍœK‚Å‚ ‚éB‚»‚Ģ‘¼‚Ģ•¶‘—į‚¦‚Īƒ‰ƒCƒuƒ‰ƒŠEƒŠƒtƒ@ƒŒƒ“ƒXEƒ}ƒjƒ…ƒAƒ‹‚Č‚Ē‚ĶPython Document‚Ų[ƒW‚ɃŠƒXƒg‚³‚ź‚Ä‚¢‚éB

The previously mentioned Python Tutorial is an excellent place to start reading if you have never used Python before. Other documentation such as the library reference manual is indexed at the Python Documentation page.

Toolbox‚ĢŽg‚¢•ū‚āPythonƒAƒvƒŒƒbƒg‚Ģģ¬‚Č‚ĒMacintosh“Į—L‚ĢŽ–€‚É‚Ā‚¢‚Ä‚ĢƒTƒ“ƒvƒ‹‚ŖŠō‚Ā‚©—pˆÓ‚³‚ź‚¢‚éB

There are some annotated sample programs available that show some mac-specific issues, like use of various toolboxes and creation of Python applets.

Python‚ĢMacintosh”z•tƒLƒbƒg‚ÉŠÜ‚Ü‚ź‚éDemo‚Ø‚ę‚ŃMac:DemoƒtƒHƒ‹ƒ_‚É‚ą‚»‚Ģ‘¼‚ĢƒTƒ“ƒvƒ‹ƒvƒƒOƒ‰ƒ€‚Ŗ—pˆÓ‚³‚ź‚Ä‚¢‚éB‚Ł‚Ę‚ń‚Ē‚ĢƒTƒ“ƒvƒ‹‚Ķ‚Ł‚ń‚Ģ‚ķ‚ø‚©‚Ģą–¾‚µ‚©ŠÜ‚Ü‚ź‚Ä‚¢‚Č‚¢B‚»‚ź‚Å‚ą‚±‚ź‚ē‚ĢƒTƒ“ƒvƒ‹‚ĶPython‚ĢŠō‚Ā‚©‚Ģ“Į’„‚É‚Ā‚¢‚Ä‚ ‚Č‚½‚Ŗ—‰š‚š[‚ß‚é‚Ģ‚É–š—§‚Ā‚¾‚ė‚¤B

The Demo and Mac:Demo folders in the Macintosh distribution contains a number of other example programs. Most of these are only very lightly documented, but they may help you to understand some aspects of using Python.

ÅŒć‚ɁA‰½l‚©‚ĢPython‚ĢŠJ”­ŽŅ‚Ģģ•i‚ŖMac:ContribƒtƒHƒ‹ƒ_‚ÉŠÜ‚Ü‚ź‚Ä‚¢‚éB‚±‚ź‚ē‚ĢƒvƒƒOƒ‰ƒ€‚Ķ’Źķ‚Ģ•Ŗ—Ž‚ÉŽū‚߂邱‚Ę‚Ķo—ˆ‚Č‚©‚Į‚½‚ŖA”z•t‚·‚邱‚Ę‚Ŗ‚Ģ‚¼‚Ü‚ź‚½‚ą‚Ģ‚¾B(ƒRƒ“ƒgƒŠƒrƒ…[ƒ^‚Ģģ•i‚ĶPython‚Ģ”z•t‚Ģ‚»‚±‚©‚µ‚±‚ÉŠÜ‚Ü‚ź‚Ä‚¢‚é‚ŖA‹C‚Ć‚­‚±‚Ę‚Ķ–³‚¢B)Macintosh PythonƒvƒƒOƒ‰ƒ€‚Ģ’‡ŠŌ‚Ę˜A—‚šŽę‚č‡‚¤‚ą‚Į‚Ę‚ą‚ę‚¢•ū–@‚ĶMacPythonSIGƒ[ƒŠƒ“ƒOƒŠƒXƒg‚ÉŽQ‰Į‚·‚邱‚Ę‚Å‚ ‚éB–{•¶‚É"info"‚ʏ‘‚©‚ź‚½ƒ[ƒ‹‚špythonmac-sig-request@python.org‚É‘—‚é‚©Awww.python.org WWW ƒT[ƒo‚ĢPythonmac SIG page ‚šŒ©‚Ä—~‚µ‚¢B
 

Finally, there is a Mac:Contrib folder that contains a few contributions to Python that I couldn't fit in the normal tree but did want to distribute (many other contributions are contained throughout the distribution, but you don't see them, really). The best way to contact fellow Macintosh Python programmers is to join the MacPython Special Interest Group mailing list. Send a message with "info" in the body to pythonmac-sig-request@python.org or view the Pythonmac SIG page on the www.python.org WWW server.

ƒgƒ‰ƒuƒ‹‚Ö‚Ģ‘Ī‰žTroubleshooting

‚æ‚å‚Į‚Ę‚¢‚ē‚¢‚ē‚Ę‚³‚¹‚éƒGƒ‰[ƒƒbƒZ[ƒW‚ĶAPythonCore‚ĶŠmŽĄ‚ɃCƒ“ƒXƒg[ƒ‹‚³‚ź‚Ä‚¢‚é‚Ģ‚É‚ą‚©‚©‚ķ‚ē‚øo‚Ä‚­‚éAPython ƒCƒ“ƒ^ƒvƒŠƒ^‹N“®Žž‚Ģ"PythonCore not found" ‚Å‚ ‚éB‚±‚ĢƒƒbƒZ[ƒW‚ĶŽĄŪ‚É‚ĶA"ƒVƒXƒeƒ€ƒq[ƒv‚ÉPythonCore‚šƒ[ƒh‚·‚é‚Ģ‚ɏ\•Ŗ‚Čƒƒ‚ƒŠ—e—Ź‚Ŗ‚Č‚¢"‚šˆÓ–”‚µ‚Ä‚¢‚éB•Ŗ‚©‚č‚É‚­‚¢ƒƒbƒZ[ƒW‚É‚Ā‚¢‚Ä‚ĶApple‚š”ń“ļ‚µ‚Ä—~‚µ‚¢B

A rather baffling error message can be "PythonCore not found" when you start the interpreter and you are sure that PythonCore is available. The message should actually say "Not enough memory in the system heap to load PythonCore". Blame Apple for the confusing message.

CFM68K”Å‚ĢPythonƒCƒ“ƒ^ƒvƒŠƒ^‚ĶQuichkTime‚É‚Ā‚¢‚Ä–ā‘č‚Ŗ‚ ‚é‚ꂤ‚Å‚ ‚éB‚ą‚µ–ā‘č‚ɏo‰ļ‚Į‚½‚ēASIG‚ɃRƒ“ƒ^ƒNƒg‚µ‚Ä—~‚µ‚¢BQuicktime‚š–ā‘č‚Č‚­Žg‚¦‚Ä‚¢‚él‚ą‚¢‚邵A‚»‚¤‚Å‚Č‚¢l‚ą‚¢‚éA‰äX‚Ķ‚»‚ĢŒ“ˆö‚š’T‚µ‹‚ß‚Ä‚¢‚éB

There appear to be problems with QuickTime for the CFM68K version of the interpreter. If you experience these please contact the SIG: some people use quicktime without problems and some not, and we are still hunting for the cause.

Python‚Ķ”äŠr“IˆĄ‘S‚ČŒ¾Œź‚Å‚ ‚éBPythonƒXƒNƒŠƒvƒg‚ŃVƒXƒeƒ€‚ĢƒCƒ“ƒ^ƒvƒŠƒ^‚šƒNƒ‰ƒbƒVƒ…‚³‚¹‚邱‚Ę‚Ķ¢“ļ‚Å‚ ‚éB‚±‚Ģƒ‹[ƒ‹‚É‚ą—įŠO‚Ŗ‚ ‚éBƒVƒXƒeƒ€ƒc[ƒ‹ƒ{ƒbƒNƒX(ƒEƒBƒ“ƒhƒEAQuickdraw‚Č‚Ē)‚Ö‚ĢƒCƒ“ƒ^[ƒtƒFƒCƒX‚š‚Ø‚±‚Č‚¤ƒ‚ƒWƒ…[ƒ‹‚Ķ”ńķ‚É‚ķ‚ø‚©‚ČƒGƒ‰[‚Ģƒ`ƒFƒbƒN‚µ‚©s‚Č‚ķ‚Č‚¢B]‚Į‚āA‚±‚ź‚ē‚Ģƒ‚ƒWƒ…[ƒ‹‚šŽg‚¤Œė‚Į‚½U‚é•‘‚¢‚š‚Ø‚±‚Č‚¤ƒvƒƒOƒ‰ƒ€‚ĶŽĄŪ‚ɃVƒXƒeƒ€‚šƒNƒ‰ƒbƒVƒ…‚³‚¹‚éB‚»‚Ģ‚ꂤ‚ČƒvƒƒOƒ‰ƒ€‚ĶŽc”O‚Č‚Ŗ‚ēƒfƒoƒbƒO‚·‚邱‚Ę‚Ŗ”ńķ‚ɍ¢“ļ‚Å‚ ‚éB‚ ‚«‚ē‚©‚É‚»‚ź‚ē‚ĢƒGƒ‰[‚Ķ•W€“I‚ČPython‚ĢƒGƒ‰[ƒƒbƒZ[ƒW‚šo—Ķ‚µ‚Č‚¢‚µAƒfƒoƒbƒO—p‚ĢƒvƒŠƒ“ƒg•¶‚ĶƒvƒƒOƒ‰ƒ€‚ĢŽĄs‚ĘŠ±Ā‚·‚é‚©‚ē‚Å‚ ‚éB‚±‚ź‚É‚Ā‚¢‚ÄŒ»Żo—ˆ‚邱‚Ę‚Ķ‚ ‚Ü‚č‚Č‚¢B
 

Python is a rather safe language, and hence it should be difficult to crash the interpreter of the system with a Python script. There is an exception to this rule, though: the modules that interface to the system toolboxes (windowing, quickdraw, etc) do very little error checking and therefore a misbehaving program using these modules may indeed crash the system. Such programs are unfortunately rather difficult to debug, since the crash does not generate the standard Python stack trace, obviously, and since debugging print statements will often interfere with the operation of the program. There is little to do about this currently.

‚Ø‚»‚ē‚­‘¼‚ĢƒVƒXƒeƒ€‚©‚ēƒ|[ƒg‚µ‚½ƒ‚ƒWƒ…[ƒ‹‚ÉŠÖ‚·‚é–ā‘č‚Ģ‚ą‚Į‚Ę‚ą‚ę‚­‚ ‚錓ˆö‚ĶMac‚Ģend-of-line‚Ģ–ń‘©‚Å‚ ‚éBUnix‚Å‚Ķƒ‰ƒCƒ“ƒtƒB[ƒh(0x0a)‚šŽg‚¤‚Ģ‚ɁA Mac‚ĶƒLƒƒƒŠƒbƒWEƒŠƒ^[ƒ“(0x0d0)‚šŽg‚¤B–ā‘č‚š‚³‚ē‚É‚ā‚ā‚±‚µ‚­‚µ‚Ä‚¢‚é‚Ģ‚ĶBBEdit“™‚ĢƒGƒfƒBƒ^‚Ģ‚Ł‚Ę‚ń‚ĒƒnA‚¢‚ø‚ź‚ĢEOLƒRƒ“ƒxƒ“ƒVƒ‡ƒ“‚Å‚ą–ā‘č‚Č‚­“®ģ‚·‚鎖‚Å‚ ‚éB‚±‚ź‚ē‚ĢƒGƒfƒBƒ^ć‚Å‚ĶAƒvƒƒOƒ‰ƒ€‚Ķ–ā‘č‚Č‚­Œ©‚¦‚é‚Ģ‚É‚»‚Ģƒ‚ƒWƒ…[ƒ‹‚šImport‚µ‚½‚Ę‚±‚ė‚ŃGƒ‰[‚É‚Č‚Į‚Ä‚µ‚Ü‚¤B
 

Probably the most common cause of problems with modules ported from other systems is the Mac end-of-line convention. Where unix uses linefeed, 0x0a, to separate lines the mac uses carriage return, 0x0d. To complicate matters more a lot of mac programming editors like BBEdit and emacs will work happily with both conventions, so the file will appear to be correct in the editor but cause strange errors when imported. BBEdit has a popup menu which allows you to inspect (and set) the end-of-line convention used in a file.

Python‚ĶPythonƒtƒHƒ‹ƒ_‚šˆŚ“®‚·‚é‚Č‚Ē‚Ģ•ĻX‚Ŗ‚ ‚Į‚½‚Ę‚µ‚Ä‚ą‚»‚Ģ‰ŠśŻ’čƒtƒ@ƒCƒ‹‚šÅV‚É‚µ‚Ä‚Ø‚±‚¤‚Ę‚·‚éB‚ą‚µ‚ą‚±‚ź‚ÉŽø”s‚·‚é‚ĘPython‚š‹N“®‚·‚邱‚Ę‚Ŗ‚Å‚«‚Č‚¢AÅˆ«‚Ģź‡‚É‚ĶPython‚Ķ‹N“®‚·‚é‚ą‚Ģ‚Ģ•W€ƒ‚ƒWƒ…[ƒ‹‚šŒ©‚Ā‚Ƃ鎖‚Ŗo—ˆ‚Č‚­‚Č‚éB‚±‚Ģź‡Python‚š‹N“®‚µ‚Äsys.path‚š’²‚ׂČ©‚ꂤB‚ą‚µ‚»‚ź‚Ŗ•s³‚Å‚ ‚ź‚ĪA‚·‚ׂĂĢPython‰ŠśŻ’čƒtƒ@ƒCƒ‹‚šƒVƒXƒeƒ€ƒtƒHƒ‹ƒ_‚©‚ēŽę‚čœ‚«AƒCƒ“ƒ^ƒvƒŠƒ^‚ŖPythonƒtƒHƒ‹ƒ_‚É‚ ‚éŠŌ‚ɁAPythonƒCƒ“ƒ^ƒvƒŠƒ^‚š‹N“®‚·‚éB‚±‚ź‚É‚ę‚Į‚āA‰ŠśŻ’čƒtƒ@ƒCƒ‹‚ŖV‚½‚É‚Ā‚­‚ē‚ź‚éB‚ ‚é‚¢‚ĶAConfigurePythonƒAƒvƒŒƒbƒg‚šÄ“x‹N“®‚µ‚Ä‚ą‚ę‚¢B

Python attempts to keep its preferences file up-to-date even when you move the Python folder around, etc. If this fails the effect will be that Python cannot start or, worse, that it does work but it cannot find any standard modules. In this case, start Python and examine sys.path. If it is incorrect remove any Python preferences file from the system folder and start the interpreter while the interpreter sits in the main Python folder. This will regenerate the preferences file. You may also have to run the ConfigurePython applet again.

‚ą‚¤ŽžŠŌŲ‚ź‚Å‚·AŽŸ‚Ģ•ū‚Ē‚¤‚¼IYour five minutes are up. Next!

The next section to check out is the annotated sample programs.


Jack Jansen, jack@cwi.nl, 27-Apr-98.
[“ś–{Œź–óFŽR–{ø, 2000”N‚RŒŽ‚X“ś]