12
UNIVERSITY OF MICHIGAN INFORMATICS Charles Severance Installing Python Increasingly a number of School of Information and EECS courses are using Python as a programming language. Python is viewed as a good combination of ease of use and powerful capabilities. Python is often used to do data manipulation and processing. If you are not planning to be a professional programmer (i.e. you are a biologist or user interface designer) and you only have time or energy to learn one programming language ‐ know one computer language – that language should be Python. This handout covers getting Python installed in a Macintosh, Windows XP, and Windows Vista systems and shows you how to run your first Python program. We will not cover how to edit the Python source code – we recommend JEdit (www.jedit.org) as a programmer’s editor. You will likely end up with problems if you try to use Notepad (Windows) or TextEdit (Macintosh) to edit your Python code. You can also use the built in Python editor (IDLE) to edit your Python programs – this has some advantages and disadvantages as well – particularly as your programs get more sophisticated. This handout will show you how to run Python programs in the terminal interface in each of the operating systems. While this might seem a little clunky at first for folks used to point and click interfaces – we find that you feel more powerful and capable in the command line interface – once you get used to it. Installing Python on a Macintosh The good news is that Python is already installed on all recent Macintosh systems. To check which version of Python you have, launch a terminal window under Applications ‐> Utilities ‐> Terminal and type the command: python –version

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

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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.