View
3
Download
0
Category
Preview:
Citation preview
UNIVERSITYOFMICHIGAN INFORMATICSCharlesSeverance
InstallingPython
IncreasinglyanumberofSchoolofInformationandEECScoursesareusingPythonasaprogramminglanguage.Pythonisviewedasagoodcombinationofeaseofuseandpowerfulcapabilities.Pythonisoftenusedtododatamanipulationandprocessing.Ifyouarenotplanningtobeaprofessional
programmer(i.e.youareabiologistoruserinterfacedesigner)andyouonlyhavetimeorenergytolearnoneprogramminglanguage‐knowonecomputerlanguage–thatlanguageshouldbePython.
ThishandoutcoversgettingPythoninstalledinaMacintosh,WindowsXP,andWindowsVistasystemsandshowsyouhowtorunyourfirstPythonprogram.WewillnotcoverhowtoeditthePythonsourcecode–werecommendJEdit(www.jedit.org)asaprogrammer’seditor.Youwilllikelyendupwith
problemsifyoutrytouseNotepad(Windows)orTextEdit(Macintosh)toedityourPythoncode.YoucanalsousethebuiltinPythoneditor(IDLE)toedityourPythonprograms–thishassomeadvantagesanddisadvantagesaswell–particularlyasyourprogramsgetmoresophisticated.
ThishandoutwillshowyouhowtorunPythonprogramsintheterminalinterfaceineachoftheoperatingsystems.Whilethismightseemalittleclunkyatfirstforfolksusedtopointandclick
interfaces–wefindthatyoufeelmorepowerfulandcapableinthecommandlineinterface–onceyougetusedtoit.
InstallingPythononaMacintoshThegoodnewsisthatPythonisalreadyinstalledonallrecentMacintoshsystems.TocheckwhichversionofPythonyouhave,launchaterminalwindowunderApplications‐>Utilities‐>Terminaland
typethecommand:python–version
YouwillbeusingTerminalalot–soyoumightwanttokeepitinyourdockatthebottomofthescreen.TodothisclickontheTerminaliconinthedockandholdforafewseconds–amenuwillpopup–select
KeepinDock–andTerminalwillalwaysbetherewithoneclicktolaunchit.YoumaywanttoupgradetheversionofPythonyouhaveonyourMacintosh–simplygoto
www.python.organddownloadandinstallanewerversion.AfterwardsmakesuretogointoyourterminalprogramandchecktoseethatyouarerunningthecorrectversionofPythonusingtheabovecommand.
MacintoshuserscanskiptheWindowsinstallationandjumptothe“RunningPythonInteractively”section.
InstallingPythononWindowsPythonisverypopularonWindowssystemsbutitdoesnotcomepre‐installedasitdoesonthe
Macintosh.Theinstallationisverysimple–butwehavetoconfiguresomesystemsettingsaftertheinstallation.Goto
http://www.python.org/download/AndsearchfortheWindowsInstaller–downloadtheinstallerandsaveittoyourDesktop.Whenthe
downloadcompletes–simplyclickontheinstallertobegintheinstallationprocess.Duringtheinstallationyoumayseeseveraldialogboxesaskingyoutotrustorapprovetheinstallerto
dosomething.Makesuretosay“yes”or“approve”theseactionssotheinstallationcancontinue.Windowsisjustbeingcautious–thetimetoworryaboutthesepop‐upsiswhen
OncethePythoninstallationiscomplete,youneedtosetituintheWindowsCommandLine.
SettingupPythonForWindowsCommandLineThisstepmakesitpossibletorunPythonfromtheWindowsCommandLine.Itisabitconvoluted‐butyoushouldbeabletofollowthestepsandgetittowork.Ifyouareuncomfortablefollowingthesesteps–getsomehelp–youonlyhavetodothisonceandthenneveragain–soyoudon’thavetoreallylearn
this.Askilledcomputerusercandothisforyouinafewsecondswiththeseinstructions.FirstyouneedtogointotheControlPanelandgettotheSystemPropertiesDialog.Therearedifferent
stepsbetweenVistaandXPtogetthSystemProperties–onceyougettoSystemProperties–thestepsarethesameforbothversionsofWindows.
WindowsVistaInitialSteps:GotoStart‐>ControlPanel(controlpanelisusuallyontherightsideoftheStartpopuponthethirdupfromthebottom).Thenintheupperrightofthecontrolpanelinthe
searcharea,typesystempath‐Vistawillpresentyouwithanoptiontitled“EdittheSystemEnvironmentVariables”.
Youwillgetasecuritydialog–thenpressContinueandmoveforwardtoseetheSystemPropertiesdialogbox.
WindowsXPInitialSteps:GotoStart‐>ControlPanel–onceyouareinControlPanel,dependingonwhetheryouareinclassicmodeornot,youmayhavetoSelectPerformanceandMaintenanceandthenclickonSystemtobringuptheSystemPropertiesdialog.
Windows–BothVersions:
PressthebuttonlabeledEnvironmentVariables–inthesectiontitledSystemVariablesscrolldownuntilyoufindtheonecalledPath.
AfteryouhaveselectedthePathvariablepresstheEditbutton–itwillbringupthefollowinginterface.
Dothisnextstepcarefully.Putyourcursorinthetextarealabeled“VariableValue”anduseyourrightcursortomovetotheveryendofthestringandthenaddthefollowingtexttotheendofthePath
string.Intheaboveexamplewewanttoaddthetextattheendofthe“Wbem”.YourexistingPathstringmaylookdifferent–allthatmattersisthatyouaddthistextattheendofwhateveristhere.
;C:\Python25Noteifyouareinstallingaversionlaterthan2.5.x,youwillneedtochangethe“25”towhatever
directorythatwasusedtoinstallPython(i.e.thepathmightbeC:\Python26)Whenyouaredone–itshouldlooklikethis.
Ifyouareunsureifyouaredoingthisright–justpressCancelabunchoftimesandgetsomehelp.Butifitlooksright(youhaveaddedthe;C:\Python25totheendofthepathstringwithnoextraspaces)–thenpressOKandsavethePathSystemVariable.TheclosetherestoftheDialogboxestoinsurethe
settingsaresaved.Thenchecktoseeifthisworkedbystartingthecommandlineinterface.
WindowsVistaInstructions:PressStart(theroundWindowiconinthelowerright)andinthespacecalledStartSearchtypeinthewordcommand–Vistawillfindthe“CommandPrompt”–selectand
launchtheCommandPrompt.
WindowsXPInstructions:TostartthecommandlineinterfacetoWindowsXP,doStart‐>Run‐>cmd‐>OK–YouwillseeaninterfacethatlooksasfollowsafteryoupressStart‐>Run–simplyentercmdand
pressOK.
WindowsBothVersionsInstructions:
Thenbringupthecommandwindowandtype
python‐‐version
Ifallhaswentwell–youshouldseethefollowingscreen.
Ifyoumakeamistake–youcantrytofixthePathvariablemorethanonce–butmakesuretocloseand
re‐openthecommandlineinterfaceeachtimeyouchangethePathvariable–sothecommandlineinterfacereloadsthenewPathVariable.YoucantypethiscommandinthecommandlinetoseethecontentsofthePathvariable:
echo%PATH%
Hopefullyyougetthroughthisstep–orgetsomeonetohelpyouthroughthestepandthenyoucanforgetaboutthisandjustusePythonfromthenon.
UsingPythonIntheWindowsCommandLineInterfaceWindowsVista:TostartthecommandlineinterfacetoWindowsVista,presstheWindowinthelowerrightandinthespacecalledStartSearchtypeinthewordcommand–Vistawillfindthe“Command
Prompt”–selectandlaunchtheCommandPrompt.
WindowsXPInstructions:TostartthecommandlineinterfacetoWindowsXP,doStart‐>Run‐>cmd‐
>OK–YouwillseeaninterfacethatlooksasfollowsafteryoupressStart‐>Run–simplyentercmdandpressOK.
OnceyouareinthecommandlineinterfaceinVistaorXP(orMacintoshforthatmatter)–simplytype
python–version
ToseewhatversionofPythonyouarerunning.ThisalsoverifiesthatPythoniscorrectlyinstalled.
RunningPythonInteractivelyYoucanusePythonwithoutactuallywritingaprogram–youcanjustrunPythonandgiveitPythoncodeinteractively.Startacommandlineinterface(Terminal,cmd,command–dependingonyouroperatingsystem)andrunPythonwithnoparameters.Youwillseea“>>>”promptwhichindicatesPythonwants
youto“talkPython”toit.Typethefollowingintoyourcommandlineorterminalwindow–yourinputsareinbold.Theexit()
commandterminatesyourinteractivePythonsession.python
Python2.5(r25:51918,Sep192006,08:49:13)[GCC4.0.1(AppleComputer,Inc.build5341)]ondarwinType"help","copyright","credits"or"license"formoreinformation.
>>>print"Hellomynameischuck"Hellomynameischuck>>>print2+2
4>>>exit()
WritingYourFirstPythonProgram(AssumingJEdit)
OpenJEditandtypeinthecodeshownbelow:
Youwillnotethered“floppydisk”iconnexttothefilenamethatindicatesthatthetexthasnotyet
beensavedintoafile.SelectFile‐>SaveAs–thennavigatetoyourDesktop.TheDesktopisdifferentforeachoperatingsystem:
Macintosh:/Users/csev/Desktop
WindowsXP:C:\DocumentsandSettings\csev\DesktopWindowsVista:C:\Users\csev\Desktop
Namethefilefirst.pyandpressSavetosavethefile.
AfteryoupressSave,JEditshouldlookasfollows(SeebelowforVistaandXP):
ThenopenthecommandlineinterfaceandswitchtotheDesktopfilesbyusingthecommandcdDesktop
Andthentype
pythonfirst.py
Itshouldlookasfollows(Macintosh):
Congratulations–youhaverunyourfirstPythonProgram–giveyourselfapatontheback.
IfyouareinWindowsVista,editingandrunningtheprogramwilllookasfollows:
IfyouarerunningWindowsXP,editingandrunningtheprogramwilllookasfollows:
ThismaterialisCopyrightCharlesSeveranceunderCreativeCommonsAttribution2.5.
Recommended