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.
To program in Python you need to install and configure a few items, which we will explain now.
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.
IDE – Sublime Text 2
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.
- Go to www.python.org and choose the ‘Download for Windows’ option for Python 3.4.
- 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.
- 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:
Next we will change directories to where we installed Python 3.4. To change directories, you use the
cdcommand, as follows:
> cd C:\Python34
To see the contents of the directory, you can type the
dircommand, and if it is too many lines to fit on one screen you can use
dir /pto enable paging.
To make sure everything is working, we will now run
python.exeto 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 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.
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!’)
You will see that the system immediately echoes back to you what you typed in. Great, everything is working!
To get out of the shell, type:
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).
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.
> 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.
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
Configuring Your Path
Typing that full path could become tedious or difficult to remember. We can set it permanently through Windows.
- Click on Start
- Right click on Computer
- Click on Properties
- In the window that opens, click on Advanced System Settings
- Click on the Advanced tab
- Click on the Environment Variables button
- In the bottom listbox, find the Path variable and left click it to highlight
- Click the Edit button
- In the Variable value text box, scroll to the end and add the following (note the semi-colon, it’s important): ;C:\Python34
- 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.
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
The python command should now work from any folder/directory on your machine.
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!