Transcript
Page 1: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

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

Page 2: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

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”.

Page 3: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

Youwillgetasecuritydialog–thenpressContinueandmoveforwardtoseetheSystemPropertiesdialogbox.

WindowsXPInitialSteps:GotoStart‐>ControlPanel–onceyouareinControlPanel,dependingonwhetheryouareinclassicmodeornot,youmayhavetoSelectPerformanceandMaintenanceandthenclickonSystemtobringuptheSystemPropertiesdialog.

Windows–BothVersions:

PressthebuttonlabeledEnvironmentVariables–inthesectiontitledSystemVariablesscrolldownuntilyoufindtheonecalledPath.

Page 4: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

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.

Page 5: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

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:

Page 6: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

Thenbringupthecommandwindowandtype

python‐‐version

Ifallhaswentwell–youshouldseethefollowingscreen.

Ifyoumakeamistake–youcantrytofixthePathvariablemorethanonce–butmakesuretocloseand

re‐openthecommandlineinterfaceeachtimeyouchangethePathvariable–sothecommandlineinterfacereloadsthenewPathVariable.YoucantypethiscommandinthecommandlinetoseethecontentsofthePathvariable:

echo%PATH%

Hopefullyyougetthroughthisstep–orgetsomeonetohelpyouthroughthestepandthenyoucanforgetaboutthisandjustusePythonfromthenon.

UsingPythonIntheWindowsCommandLineInterfaceWindowsVista:TostartthecommandlineinterfacetoWindowsVista,presstheWindowinthelowerrightandinthespacecalledStartSearchtypeinthewordcommand–Vistawillfindthe“Command

Prompt”–selectandlaunchtheCommandPrompt.

Page 7: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

WindowsXPInstructions:TostartthecommandlineinterfacetoWindowsXP,doStart‐>Run‐>cmd‐

>OK–YouwillseeaninterfacethatlooksasfollowsafteryoupressStart‐>Run–simplyentercmdandpressOK.

OnceyouareinthecommandlineinterfaceinVistaorXP(orMacintoshforthatmatter)–simplytype

python–version

ToseewhatversionofPythonyouarerunning.ThisalsoverifiesthatPythoniscorrectlyinstalled.

Page 8: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

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:

Page 9: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

Macintosh:/Users/csev/Desktop

WindowsXP:C:\DocumentsandSettings\csev\DesktopWindowsVista:C:\Users\csev\Desktop

Namethefilefirst.pyandpressSavetosavethefile.

AfteryoupressSave,JEditshouldlookasfollows(SeebelowforVistaandXP):

ThenopenthecommandlineinterfaceandswitchtotheDesktopfilesbyusingthecommandcdDesktop

Page 10: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

Andthentype

pythonfirst.py

Itshouldlookasfollows(Macintosh):

Congratulations–youhaverunyourfirstPythonProgram–giveyourselfapatontheback.

Page 11: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

IfyouareinWindowsVista,editingandrunningtheprogramwilllookasfollows:

Page 12: UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Pythoncsev/courses/shared/handouts/old/Installi… · UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing

IfyouarerunningWindowsXP,editingandrunningtheprogramwilllookasfollows:

ThismaterialisCopyrightCharlesSeveranceunderCreativeCommonsAttribution2.5.


Recommended