Hello World in Python on Microsoft Windows

Your First Program in Python 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 7, but the instructions for Windows 8, Windows Vista, and Windows XP are similar.



Overview

The Python programming environment required by this booksite consists of:

At the time of writing, some parts of the Pygame library still have not been ported to Python 3. So the instructions that follow are for Python 2, not Python 3.



Downloading and Installing Python, IDLE, and Tkinter

Perform these steps to download and install Python, IDLE, and Tkinter:

Perform these steps to test your installation:



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:

Perform these steps to make sure that you have set the PATH environment variable properly:



Downloading and Installing NumPy

Perform these steps to download and install the NumPy library:

Perform these steps to test your installation of NumPy:



Downloading and Installing Pygame

Perform these steps to download and install the Pygame library:

Perform these steps to test your installation of Pygame:



Downloading and Installing the Booksite Library

Perform these steps to download and install the booksite library:

Perform these steps to test your installation of the booksite library:



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:



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:



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:

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:

Then perform these steps to test your installation of the booksite example programs:



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:



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:

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. Can I use newer versions of Python, the Python standard library, IDLE, Tkinter, NumPy, and Pygame?

A. Yes. The above instructions tell you how to install Python 2.7.9 and versions of the accompanying software that are compatible with Python 2.7.9. It is OK to install newer versions; but you must make sure they're compatible. This is the procedure:

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 the Microsoft Windows Explorer to reveal the .py file extensions?

A. On many Windows machines the Explorer is configured to hide the file extensions. To override this default, select Start → My Computer → Tools → Folder Options → View, uncheck the box next to Hide file extensions for known file types, and click OK.

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.