Your First Program in Python 2 on Microsoft Windows
This page tells you how to setup a Python programming environment for your Microsoft Windows computer and provides a step-by-step guide for composing and running a simple "Hello, world" Python program. All of the software is freely available on the Web. These instructions are for Windows 10, but the instructions for other recent versions of Windows are similar.
Overview
The Python programming environment required by this booksite consists of:
- Python, that is, the Python compiler/interpreter.
- The Python standard libraries.
- IDLE, the Python Integrated DeveLopment Environment.
- The Tkinter, NumPy, and Pygame libraries, which are used by the booksite programs that do graphics or audio processing.
- The booksite library, that is, a set of modules that we developed specifically to support this booksite.
- The Command Prompt application that is bundled with Microsoft Windows.
Downloading and Installing Python, IDLE, Tkinter, NumPy, and PyGame
Note: The Windows Explorer application uses the term folder to mean a container of documents and, perhaps, other folders. This document instead uses the equivalent Unix term directory.
Perform these steps to download and install Python, IDLE, Tkinter, NumPy, and PyGame:
-
Browse to the Python for Windows download page.
-
In the resulting page, under the heading Python 2.7.14 - 2017-09-16 click on Windows x86 MSI installer. Your browser will download the file
python-2.7.14.msi
to your Downloads directory. -
Using File Manager, navigate to your Downloads directory, and double-click on the
python-2.7.14.msi
file to install Python, Tkinter, and IDLE Use the default options, but make sure that the Add Python 2.7 to PATH checkbox is checked. (It's OK to delete thepython-2.7.14.msi
file after the installation is complete.) -
Open a Command Prompt window. To do that, in the Type here to search area type Command Prompt. Then in the resulting popup menu click on Command Prompt. You should see a new window containing text like this:
-
Microsoft Windows [Version 10.0.15063] Copyright (c) 2017 Microsoft Corporation. All rights reserved.
In the Command Prompt window issue these commands to install NumPy and PyGame:
-
python -m pip install -U numpy --user python -m pip install -U pygame --user
To "issue a command" you should type the command followed by the Enter key.
Perform these steps to test your installation:
-
In the Command Prompt window issue the
C:\python27\python
command. You should see something like this: -
Python 2.7.14 Type "help", "copyright", "credits" or "license" for more information. >>>
-
If you see that output, then your installation of Python, IDLE, and Tkinter was successful.
-
At the Python
>>>
prompt, typeimport numpy
followed by theEnter
key. If no error messages appear, then your installation of NumPy was successful. -
At the Python
>>>
prompt, typeimport pygame
followed by theEnter
key. If no error messages appear, then your installation of PyGame was successful. -
At the Python
>>>
prompt, typeexit()
followed by theEnter
key to exit Python. -
Close the Command Prompt window.
Setting the PATH
You will need to issue the C:\Python27\python
command frequently to run your Python programs. Doing so will be tedious; you will much prefer to issue the abbreviated python
command instead. You can use that abbreviation if and only if you add the directory C:\Python27
to your PATH
environment variable. Perform these steps to do that:
In the Type here to search area type "Control Panel".
In the resulting popup dialog box click on Control Panel.
-
In the Control Panel click on System and Security → System → Advanced system settings → Environment Variables → System variables → PATH.
-
Click on the Edit... button.
-
Add
C:\Python27;
(note the trailing semicolon) to the beginning of thePATH
environment variable. -
Click the OK buttons three times.
Perform these steps to make sure that you have set the PATH environment variable properly:
-
If any Command Prompt windows currently are open, then close them.
Open a Command Prompt window.
-
Issue the
python
command. You should see something like this: -
Python 2.7.14 Type "help", "copyright", "credits", or "license" for more information. >>>
-
If you see that output, then you have set the
PATH
environment variable properly. -
At the Python
>>>
prompt, typeexit()
followed by theEnter
key to exit Python. -
Close the Command Prompt window.
Downloading and Installing the Booksite Library
Perform these steps to download and install the booksite library:
-
Use your Web browser to download this introcs-1.0.zip file to your
C:\Users\yourusername\Downloads
directory. -
In Windows Explorer, right-click on the
C:\Users\yourusername\Downloads\introcs-1.0.zip
file, and select Extract All... from the popup menu. In the resulting dialog box, typeC:\Users\yourusername\Downloads
as the destination directory, and click on the Extract button thus creating a directory namedC:\Users\yourusername\Downloads\introcs-1.0
. (It's OK to delete theintrocs-1.0.zip
file after you have extracted all files from it.) -
Open a Command Prompt window.
-
At any time the Command Prompt application has a working directory. Initially the working directory is
C:\Users\yourusername
. In the Command Prompt window issue thecd Downloads
command to change your working directory toC:\Users\yourusername\Downloads
, and then issue thecd introcs-1.0
command to change your working directory toC:\Users\yourusername\Downloads\introcs-1.0
. (Incidentally, thecd ..
command changes your working directory to the "parent" of the current working directory.) -
Issue the
dir
command to display the names of all files in your working directory. Make sure that a file namedsetup.py
is in your working directory. -
Issue the
python setup.py install --user
command. The computer copies the files defining the booksite modules to a directory where Python can find them, and writes status messages to your Command Prompt window to indicate its progress.
Perform these steps to test your installation of the booksite library:
-
In the Command Prompt window issue the
python
command. -
At the Python
>>>
prompt, type the statementimport stdio
followed by the Enter key. If Python generates no error messages, then you have installed the standard booksite library properly. -
At the Python
>>>
prompt, typeexit()
followed by theEnter
key to exit Python. -
Close the Command Prompt window.
Configuring IDLE
So far you've downloaded and installed all of the software that you'll need. You should perform one more step before creating your first program: configure the IDLE programming environment. Follow these instructions:
-
In the Type here to search area type
idle
. -
In the resulting popup menu click on IDLE.
-
Click on the Options → Configure IDLE menu item.
-
Click on the General tab.
-
Click on the Open Edit Window radio button.
-
Click on the Ok button.
-
Close the IDLE window.
Composing Your First Program
Having installed Python, the Python standard libraries, IDLE, Tkinter, NumPy, Pygame, and the booksite libraries, and having configured IDLE, you are ready to compose your first Python program. Perform these instructions:
-
Using Windows Explorer, create a directory named
C:\Users\yourusername\hello
. -
Open IDLE as described above.
-
In IDLE, type the four-line Python program helloworld.py exactly as it appears below. Use the arrow keys, mouse, or touchpad to move within the text that you have typed. Use the Backspace or Delete key to delete text. Be careful; the smallest typing mistake might cause the program to fail.
-
import stdio # Write 'Hello, World' to standard output. stdio.writeln('Hello, World')
-
When you are finished typing, in IDLE click on the File → Save menu item to save the program. Save it in a file named
helloworld.py
in the directoryC:\Users\yourusername\hello
. The file name is case sensitive, so make sure you use all lowercase letters.
Running Your First Program
The final step is to run your program. It is possible to run some Python programs from within IDLE, but you should run the programs associated with this booksite directly from a Command Prompt window. To do that, perform these steps:
-
Open a Command Prompt window.
-
Issue the
cd hello
command to make theC:\Users\yourusername\hello
directory your working directory. -
Issue the
dir
command to show the names of all files in the working directory. Confirm that the working directory contains yourhelloworld.py
file. -
Issue the
python helloworld.py
command to run your program. If the computer writes "Hello, World" to the Command Prompt window, then the execution of yourhelloworld.py
program was successful. If the computer instead writes error messages, then use IDLE to correct your program, and issue thepython helloworld.py
command again. Repeat until your program runs successfully. If your program runs successfully the first time you try, then intentionally introduce an error into your program, just so you get some experience with correcting errors. -
Close the IDLE window.
-
Close the Command Prompt window.
You now have installed and configured a reasonable Python environment, and have used it to compose and run a Python program. Congratulations! You are a Python programmer!
Downloading the Booksite Example Programs (optional)
We recommend that you download the booksite example programs, that is, the example Python programs that are presented incrementally throughout the booksite. Having done so, you can run those programs to help you learn about them. Perform these instructions:
-
Use your Web browser to download this introcs-python.zip file to your
Downloads
directory. -
In Windows Explorer, right-click on the
C:\Users\yourusername\Downloads\introcs-python.zip
file, and select Extract All... from the popup menu. In the resulting dialog box, typeC:\Users\yourusername\Downloads
as the destination directory, and click on the Extract button thus creating a directory namedC:\Users\yourusername\Downloads\introcs-python
. (It's OK to delete theintrocs-python.zip
file after you have extracted all files from it.)
Then perform these steps to test your installation of the booksite example programs:
-
Open a Command Prompt window.
-
Issue the
cd Downloads
andcd introcs-python
commands to makeC:\Users\yourusername\Downloads\introcs-python
your working directory. -
Issue the
dir
command. Confirm that the working directory contains a file namedbouncingball.py
. -
Issue the
python bouncingball.py
command. If Python launches a stddraw window showing an animated bouncing ball, then your installation of the booksite examples was successful. -
Close the stddraw window.
-
Close the Command Prompt window.
Downloading the Booksite Example Data (optional)
We recommend that you download the booksite example data, that is, the data files used by the Python programs that are presented incrementally throughout the booksite. Perform these instructions:
-
Use your Web browser to download this introcs-data.zip file to your
C:\Users\yourusername\Downloads
directory. -
In Windows Explorer, right-click on the
C:\Users\yourusername\Downloads\introcs-data.zip
file, and select Extract All... from the popup menu. In the resulting dialog box, typeC:\Users\yourusername\Downloads
as the destination directory, and click on the Extract button thus creating a directory namedC:\Users\yourusername\Downloads\introcs-data
containing the booksite example data files. (It's OK to delete theintrocs-data.zip
file after you have extracted all files from it.)
Downloading the Booksite Library: Part 2 (optional)
Previously on this page we described how to download and install the booksite library so Python can find it. Now we describe how to download the booksite library so you can find it — for the sake of studying the code that implements it, should you so desire. Perform these instructions:
-
Use your Web browser to download this stdlib-python.zip file to your
C:\Users\yourusername\Downloads
directory. -
In Windows Explorer, right-click on the
C:\Users\yourusername\Downloads\stdlib-python.zip
file, and select Extract All... from the popup menu. In the resulting dialog box, typeC:\Users\yourusername\Downloads
as the destination directory, and click on the Extract button thus creating a directory namedC:\Users\yourusername\Downloads\stdlib-python
containing the booksite library. (It's OK to delete thestdlib-python.zip
file after you have extracted all files from it.)
We invite you to study the code that implements the booksite library. But don't be concerned if some of the code is cryptic. The code that implements the booksite library uses some features of Python that are beyond the scope of the textbook and this booksite.
Q & A
Q. How do I break out of an infinite loop when running my program from the Command Prompt?
A. Type Ctrl-c. That is, while pressing the Ctrl key, type the c key.
Q. How can I convince Microsoft Windows Explorer to reveal the .py
file extensions?
A. On many Windows machines Explorer is configured to hide the file extensions. To override this default, within Explorer select File → Change folder and search options → View, uncheck the box next to Hide file extensions for known file types, and click Apply to Folders.
Q. I downloaded files using my browser, but can't find them. Where are they?
A. Many browsers by default place downloaded files in the directory C:\Users\yourusername\Downloads
.
Q. Must I use IDLE to create my Python programs? Can I use some other text editor?
A. You need not use IDLE to create your Python programs; it is fine to use some other text editor. For example, it is reasonable to use the Notepad editor that is bundled with Microsoft Windows. However if you do use some other text editor, then make sure you change its settings so it (1) uses a four-space indentation scheme, and (2) indents using spaces instead of tabs. The Wikipedia Comparison of text editors page provides summary descriptions of many text editors.