How do I run a Python program under Windows_.pdf

3

Click here to load reader

Transcript of How do I run a Python program under Windows_.pdf

  • 9/26/12 How do I run a Python program underWindows?

    1/3effbot.org/pyfaq/how-do-i-run-a-python-program-under-windows.htm

    How do I run a Python programunder Windows?

    This is not necessarily a straightforward question. If y ou are already familiar withrunning programs from the Windows command line then every thing will seemobvious; otherwise, y ou might need a little more guidance. There are also differencesbetween Windows 95, 98, NT, ME, 2000 and XP which can add to the confusion.

    Unless y ou use some sort of integrated development environment, y ou will end uptyping Windows commands into what is variously referred to as a DOS window orCommand prompt window. Usually y ou can create such a window from y our Startmenu; under Windows 2000 the menu selection is Start | Programs | Accessories |Command Prompt. Y ou should be able to recognize when y ou have started such awindow because y ou will see a Windows command prompt, which usually looks likethis:

    C:\>

    The letter may be different, and there might be other things after it, so y ou might justas easily see something like:

    D:\Steve\Projects\Python>

    depending on how y our computer has been set up and what else y ou have recentlydone with it. Once y ou have started such a window, y ou are well on the way to runningPy thon programs.

    Y ou need to realize that y our Py thon scripts have to be processed by anotherprogram called the Py thon interpreter. The interpreter reads y our script, compiles itinto by tecodes, and then executes the by tecodes to run y our program. So, how do y ouarrange for the interpreter to handle y our Py thon?

    First, y ou need to make sure that y our command window recognises the wordpy thon as an instruction to start the interpreter. If y ou have opened a commandwindow, y ou should try entering the command py thon and hitting return. Y ou shouldthen see something like:

    Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32

    Type "help", "copyright", "credits" or "license" for more information.

    >>>

    Y ou have started the interpreter in interactive mode. That means y ou can enterPy thon statements or expressions interactively and have them executed or evaluatedwhile y ou wait. This is one of Py thons strongest features. Check it by entering a fewexpressions of y our choice and seeing the results:

    >>> print "Hello"

    Hello

    >>> "Hello" * 3

    HelloHelloHello

    Many people use the interactive mode as a convenient y et highly programmablecalculator. When y ou want to end y our interactive Py thon session, hold the Ctrl keydown while y ou enter a Z, then hit the Enter key to get back to y our Windowscommand prompt.

  • 9/26/12 How do I run a Python program underWindows?

    2/3effbot.org/pyfaq/how-do-i-run-a-python-program-under-windows.htm

    Y ou may also find that y ou have a Start-menu entry such as Start | Programs | Py thon2.2 | Py thon (command line) that results in y ou seeing the >>> prompt in a newwindow. If so, the window will disappear after y ou enter the Ctrl-Z character; Windowsis running a single py thon command in the window, and closes it when y outerminate the interpreter.

    If the py thon command, instead of display ing the interpreter prompt >>>, gives y ou amessage like:

    'python' is not recognized as an internal or external command,

    operable program or batch file.

    or:

    Bad command or filename

    then y ou need to make sure that y our computer knows where to find the Py thoninterpreter. To do this y ou will have to modify a setting called PATH, which is a list ofdirectories where Windows will look for programs. Y ou should arrange for Py thonsinstallation directory to be added to the PATH of every command window as it starts.If y ou installed Py thon fairly recently then the command

    dir C:\py*

    will probably tell y ou where it is installed; the usual location is something likeC:\Py thon23. Otherwise y ou will be reduced to a search of y our whole disk useTools | Find or hit the Search button and look for py thon.exe. Supposing y oudiscover that Py thon is installed in the C:\Py thon23 directory (the default at the timeof writing), y ou should make sure that entering the command

    c:\Python23\python

    starts up the interpreter as above (and dont forget y oull need a CTRL-Z and anEnter to get out of it). Once y ou have verified the directory , y ou need to add it to thestart-up routines y our computer goes through. For older versions of Windows theeasiest way to do this is to edit the C:\AUTOEXEC.BAT file. Y ou would want to add aline like the following to AUTOEXEC.BAT:

    PATH C:\Python23;%PATH%

    For Windows NT, 2000 and XP, y ou will need to add a string such as

    ;C:\Python23

    to the current setting for the PATH environment variable, which y ou will find in theproperties window of My Computer under the Advanced tab. Note that if y ou havesufficient priv ilege y ou might get a choice of installing the settings either for theCurrent User or for Sy stem. The latter is preferred if y ou want every body to be able torun Py thon on the machine.

    If y ou arent confident doing any of these manipulations y ourself, ask for help! At thisstage y ou may want to reboot y our sy stem to make absolutely sure the new setting hastaken effect. Y ou probably wont need to reboot for Windows NT, XP or 2000. Y ou canalso avoid it in earlier versions by editing the fileC:\WINDOWS\COMMAND\CMDINIT.BAT instead of AUTOEXEC.BAT.

    Y ou should now be able to start a new command window, enter py thon at the C:> (orwhatever) prompt, and see the >>> prompt that indicates the Py thon interpreter isreading interactive commands.

  • 9/26/12 How do I run a Python program underWindows?

    3/3effbot.org/pyfaq/how-do-i-run-a-python-program-under-windows.htm

    Lets suppose y ou have a program called py test.py in directoryC:\Steve\Projects\Py thon. A session to run that program might look like this:

    C:\> cd \Steve\Projects\Python

    C:\Steve\Projects\Python> python pytest.py

    Because y ou added a file name to the command to start the interpreter, when it startsup it reads the Py thon script in the named file, compiles it, executes it, and terminates,so y ou see another C:\> prompt. Y ou might also have entered

    C:\> python \Steve\Projects\Python\pytest.py

    if y ou hadnt wanted to change y our current directory .

    Under NT, 2000 and XP y ou may well find that the installation process has alsoarranged that the command py test.py (or, if the file isnt in the current directory ,C:\Steve\Projects\Py thon\py test.py ) will automatically recognize the .py extensionand run the Py thon interpreter on the named file. Using this feature is fine, but someversions of Windows have bugs which mean that this form isnt exactly equivalent tousing the interpreter explicitly , so be careful.

    The important things to remember are:

    1 . Start Py thon from the Start Menu, or make sure the PATH is set correctly soWindows can find the Py thon interpreter.

    python

    should give y ou a >>> prompt from the Py thon interpreter. Dont forget theCTRL-Z and ENTER to terminate the interpreter (and, if y ou started the windowfrom the Start Menu, make the window disappear).

    2. Once this works, y ou run programs with commands:

    python {program-file}

    3. When y ou know the commands to use y ou can build Windows shortcuts to runthe Py thon interpreter on any of y our scripts, naming particular workingdirectories, and adding them to y our menus. Take a look at

    python --help

    if y our needs are complex.

    4. Interactive mode (where y ou see the >>> prompt) is best used for checking thatindiv idual statements and expressions do what y ou think they will, and fordeveloping code by experiment.

    For more on running Py thon scripts on Windows, see this entry :

    how-do-i-m ake-py thon-scripts-executable

    CATEGORY : windows

    this page was rendered by a django application in 0.01s 2012-09-2417 :53:36.348012. hosted by webfaction.