Anda di halaman 1dari 3

9/26/12

How do I run a Python program underWindows?

How do I run a Python program


under Windows?
This is not necessarily a straightforward question. If y ou are already familiar with
running programs from the Windows command line then ev ery thing will seem
obv ious; otherwise, y ou might need a little more guidance. There are also differences
between Windows 95, 98, NT, ME, 2000 and XP which can add to the confusion.
Unless y ou use some sort of integrated dev elopment env ironment, y ou will end up
typing Windows commands into what is v ariously referred to as a DOS window or
Command prompt window. Usually y ou can create such a window from y our Start
menu; under Windows 2000 the menu selection is Start | Programs | Accessories |
Command Prompt. Y ou should be able to recognize when y ou hav e started such a
window because y ou will see a Windows command prompt, which usually looks like
this:
C:\>

The letter may be different, and there might be other things after it, so y ou might just
as easily see something like:
D:\Steve\Projects\Python>

depending on how y our computer has been set up and what else y ou hav e recently
done with it. Once y ou hav e started such a window, y ou are well on the way to running
Py thon programs.
Y ou need to realize that y our Py thon scripts hav e to be processed by another
program called the Py thon interpreter. The interpreter reads y our script, compiles it
into by tecodes, and then ex ecutes the by tecodes to run y our program. So, how do y ou
arrange for the interpreter to handle y our Py thon?
First, y ou need to make sure that y our command window recognises the word
py thon as an instruction to start the interpreter. If y ou hav e opened a command
window, y ou should try entering the command py thon and hitting return. Y ou should
then 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 hav e started the interpreter in interactiv e mode. That means y ou can enter
Py thon statements or ex pressions interactiv ely and hav e them ex ecuted or ev aluated
while y ou wait. This is one of Py thons strongest features. Check it by entering a few
ex pressions of y our choice and seeing the results:
>>> print "Hello"
Hello
>>> "Hello" * 3
HelloHelloHello

Many people use the interactiv e mode as a conv enient y et highly programmable
calculator. When y ou want to end y our interactiv e Py thon session, hold the Ctrl key
down while y ou enter a Z, then hit the Enter key to get back to y our Windows
command prompt.
effbot.org/pyfaq/how-do-i-run-a-python-program-under-windows.htm

1/3

9/26/12

How do I run a Python program underWindows?

Y ou may also find that y ou hav e a Start-menu entry such as Start | Programs | Py thon
2.2 | Py thon (command line) that results in y ou seeing the >>> prompt in a new
window. If so, the window will disappear after y ou enter the Ctrl-Z character; Windows
is running a single py thon command in the window, and closes it when y ou
terminate the interpreter.
If the py thon command, instead of display ing the interpreter prompt >>>, giv es y ou a
message 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 thon
interpreter. To do this y ou will hav e to modify a setting called PATH, which is a list of
directories where Windows will look for programs. Y ou should arrange for Py thons
installation directory to be added to the PATH of ev ery 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 like
C:\Py thon23. Otherwise y ou will be reduced to a search of y our whole disk use
Tools | Find or hit the Search button and look for py thon.ex e. Supposing y ou
discov er that Py thon is installed in the C:\Py thon23 directory (the default at the time
of writing), y ou should make sure that entering the command
c:\Python23\python

starts up the interpreter as abov e (and dont forget y oull need a CTRL-Z and an
Enter to get out of it). Once y ou hav e v erified the directory , y ou need to add it to the
start-up routines y our computer goes through. For older v ersions of Windows the
easiest way to do this is to edit the C:\AUTOEXEC.BAT file. Y ou would want to add a
line 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 env ironment v ariable, which y ou will find in the
properties window of My Computer under the Adv anced tab. Note that if y ou hav e
sufficient priv ilege y ou might get a choice of installing the settings either for the
Current User or for Sy stem. The latter is preferred if y ou want ev ery body to be able to
run Py thon on the machine.
If y ou arent confident doing any of these manipulations y ourself, ask for help! At this
stage y ou may want to reboot y our sy stem to make absolutely sure the new setting has
taken effect. Y ou probably wont need to reboot for Windows NT, XP or 2000. Y ou can
also av oid it in earlier v ersions by editing the file
C:\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:> (or
whatev er) prompt, and see the >>> prompt that indicates the Py thon interpreter is
reading interactiv e commands.

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

2/3

9/26/12

How do I run a Python program underWindows?

Lets suppose y ou hav e a program called py test.py in directory


C:\Stev e\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 starts
up it reads the Py thon script in the named file, compiles it, ex ecutes it, and terminates,
so y ou see another C:\> prompt. Y ou might also hav e 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 also
arranged that the command py test.py (or, if the file isnt in the current directory ,
C:\Stev e\Projects\Py thon\py test.py ) will automatically recognize the .py ex tension
and run the Py thon interpreter on the named file. Using this feature is fine, but some
v ersions of Windows hav e bugs which mean that this form isnt ex actly equiv alent to
using the interpreter ex plicitly , 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 so
Windows can find the Py thon interpreter.
python

should giv e y ou a >>> prompt from the Py thon interpreter. Dont forget the
CTRL-Z and ENTER to terminate the interpreter (and, if y ou started the window
from 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 run
the Py thon interpreter on any of y our scripts, naming particular working
directories, and adding them to y our menus. Take a look at
python --help

if y our needs are complex .


4. Interactiv e mode (where y ou see the >>> prompt) is best used for checking that
indiv idual statements and ex pressions do what y ou think they will, and for
dev eloping code by ex periment.
For more on running Py thon scripts on Windows, see this entry :
how-do-i-m ake-py thon-scripts-ex ecutable
CATEGORY : windows

this page was rendered by a django application in 0.01 s 201 2-09-24


1 7 :53:36.34801 2. hosted by webfaction.

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

3/3

Anda mungkin juga menyukai