Hello World via Python Script

Setting Up Python 3 on Windows 7

Getting a development environment up and running can often be a tedious task and even the best developers loathe setting up a new machine. But don’t worry, we will take things easy at first and have you ready to start coding in no time!

Learning Goals From this Post

This guide will walk you through how to configure Python for your Windows 7 PC. We previously covered how to set Python up on Mac, and if you are looking for that article you can find it here.

Requirements

To program in Python you need to install and configure a few items, which we will explain now.

  1. Runtime Environment – Python 3.4

    First, you need a runtime environment. This is the program that runs behind the scenes and makes YOUR programs work. For this tutorial, we will focus on Python 3.4, but you can install any version you want in the future, and can even have multiple versions of Python installed side by side.

  2. IDE – Sublime Text 2

    The second thing you need is a tool to write your programs. Programmers call these IDEs (interactive development environments). There are many options for IDEs and the debate can be heated over which is the best, but a good free option that we will use is called Sublime Text. Sublime Text is a code editor that is capable of understanding many languages. Once you master Python you can continue using it to write code in HTML, JavaScript, etc. The reason an IDE is important, instead of using a regular word processor, is because of syntax highlighting. Many languages have special keywords that will tell the program what to do. If you use an IDE it will color code the text as you write it, making it much easier to read and follow along.

    Sublime Text Syntax Highlighting
    An example of syntax highlighting provided by Sublime Text

Setting Up Your Local Environment

Installing Python 3

Unlike many computers running iOS or Linux, Windows machines do not come with a version of Python pre-installed. To get Python, we will need to go to the Python webpage, download and install the runtime.

  1. Go to www.python.org and choose the ‘Download for Windows’ option for Python 3.4.

    Official Python Page
    The official Python page, with the option to download the runtime for various operating systems
  2. When the download is complete, run the MSI which will install Python on your system. You can leave all the settings with their default values. For example, if you leave the installation directory as C:\Python34\ you can later choose to install another version of Python, and it will not conflict with the version installed now.

    The Python Installer
    The default settings in the Python Installer for Windows
  3. Next, we will test the installation by opening a command prompt. If you are not familiar with the command prompt, that’s OK. As you use Python you will quickly get a grasp of the command line. It allows you to do many of the same things that you can accomplish through Windows Explorer, such as navigate and create directories and files and run programs.To open the command prompt, click on ‘Start’ and in the search box, type cmd, and press Enter. You should be presented with a screen that looks like this:
    Search for CMD
    Search the Start Menu for the CMD prompt
    Default Command Prompt
    The default command prompt immediately after opening it.

    Next we will change directories to where we installed Python 3.4. To change directories, you use the cd command, as follows:

    > cd C:\Python34

    To see the contents of the directory, you can type the dir command, and if it is too many lines to fit on one screen you can use dir /p to enable paging.

    To make sure everything is working, we will now run python.exe to invoke the ‘Interactive Shell’. The interactive shell is an environment in which you can manually type Python code, and get an immediate response back from the runtime. If you run the following command, you should get a prompt like this:

    > python
    Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    Launching Python Shell
    The commands to open the python interactive shell

    In Programming, there is a convention that your first program is always a ‘Hello World’ program, which you use to make sure the system is alive and working. Type the following into the interactive shell:

    >>> print(‘Hello World!’)
    Hello World!

    You will see that the system immediately echoes back to you what you typed in. Great, everything is working!

    Hello World! from Python Shell
    The Hello World tradition via the Python Shell

    To get out of the shell, type:

    >>> exit()

Installing Sublime Text

To install Sublime Text, navigate to the download page at http://www.sublimetext.com/2 and choose the Windows version that is appropriate for your operating system (32 or 64 bit).

Sublime Text Web Page
Sublime Text Web Page

When the download has finished, run the executable and install the editor. It is OK to leave all the default settings, and click ‘Next’ until complete.

When the installer is finished, try and open Sublime Text to make sure the installation was successful. Click ‘Start’ and in the search box type ‘Sublime.’ Click the Sublime Text 2 option that appears.

Running Your First Program

Now we are going to write a brand new program from scratch. To do this, open a fresh, new Command Prompt. Make a new directory, using the mkdir command. We’ll call it PythonCode:

> mkdir PythonCode
> cd PythonCode

In this directory, we will create a new file using the type command, and then open explorer to see the file:

> type NUL > hello_world.py

If you see a Windows Security warning, like the one below, just hit “close” and ignore it.

Hello World Python Security Warning
Ignore Any Security Warnings

Then Type

> explorer .

Drag and drop the hello_world.py file into your Sublime Text window to start editing it. Let’s recreate our Hello World code from earlier, so that we can run it as many times as we want.

Hello World in Sublime Text
Hello World in Sublime Text

After you have added the line, save the file and go back to your command prompt. Since we are not in the C:\Python34 folder any more, we need to supply the full path to the Python runtime in order to execute the program. Try the following:

> C:\Python34\python.exe hello_world.py
Hello World!

Hello World via Python Script
Hello World via Python Script

Configuring Your Path

Typing that full path could become tedious or difficult to remember. We can set it permanently through Windows.

  1. Click on Start
  2. Right click on Computer
  3. Click on Properties
  4. In the window that opens, click on Advanced System Settings
  5. Click on the Advanced tab
  6. Click on the Environment Variables button
  7. In the bottom listbox, find the Path variable and left click it to highlight
  8. Click the Edit button
  9. In the Variable value text box, scroll to the end and add the following (note the semi-colon, it’s important): ;C:\Python34
  10. Click OK and close out all windows

That was a lot of steps to this, so follow along with the image below (click for a larger version) if you need more help.

Add python to your Path variable
Add python to your Path variable

After completing these steps, close your command prompt window and open a fresh one. This is necessary to reload it with the updated PATH variables.

You should now be able to type a more simplified command to run your program.


> cd PythonCode
> python hello_world.py
Hello, World!

The python command should now work from any folder/directory on your machine.

Wrapping Up

There you go! You have written your first Hello World Python program, and saved it in hello_world.py. You can write many more programs the same way you did this time. Just keep reading to find out how!