您在這裡

Python Programming on Windows

24 二月, 2015 - 15:30

In this appendix, we walk through a series of steps so you can run Python on Windows. There are many different approaches you can take, and this is just one approach to keep things simple.

First, you need to install a programmer editor. You do not want to use Notepad or Microsoft Word to edit Python programs. Programs must be in ”flat-text” files and so you need an editor that is good at editing text files.

Our recommended editor for Windows is NotePad++ which can be downloaded and installed from:

http://sourceforge.net/projects/notepad-plus/files/

Then download a recent version of Python 2 from the Python web site. http://www.python.org/download/releases/2.7.5/

Once you have installed Python, you should have a new folder on your computer like C:\Python27.

To create a Python program, run NotePad++ from the Start Menu and save the file with a suffix of “.py”. For this exercise, put a folder on your Desktop named py4inf. It is best to keep your folder names short and not to have any spaces in your folder or file name.

Lets make our first Python program be:

print 'Hello Chuck'

Except that you should change it to be your name. Lets save the file into Desktop\py4inf\prog1.py.

The run the command line. Different versions of Windows do this differently:

  • Windows Vista and Windows-7: Press Start and then in the command search window enter the word command and press enter.
  • Windows-XP: Press Start, then Run, and then enter cmd in the dialog box and press OK.

You will find yourself in a text window with a prompt that tells you what folder you are currently “in”.

Windows Vista and Windows-7: C:\Users\csev

Windows XP: C:\Documents and Settings\csev

This is your “home directory”. Now we need to move into the folder where you have saved your Python program using the following commands:

C:\Users\csev\> cd Desktop
C:\Users\csev\Desktop> cd py4inf

Then type

C:\Users\csev\Desktop> dir

To list your files. You should see the prog1.py when you type the dir command.

To run your program, simply type the name of your file at the command prompt and press enter.

C:\Users\csev\Desktop\py4inf> prog1.py
Hello Chuck
C:\Users\csev\Desktop\py4inf>

You can edit the file in NotePad++, save it and then switch back to the command line and execute the program again by typing the file name again at the command line prompt.

If you get confused in the command line window - just close it and start a new one.

Hint: You can also press the “up-arrow” in the command line to scroll back and run a previously entered command again.

You should also look in the preferences for NotePad++ and set it to expand tab characters to be four spaces. It will save you lots of effort looking for indentation errors.

You can also find further information on editing and running Python programs at www.py4inf.com.