Published on

Installing Python on Windows and Setting Up the Environment

Authors

Whenever I set up a new Windows PC, I always install Python. The other day I also had to build a new server machine at work, and I installed Python there as well, so I wanted to summarize the process.

Python is a scripting language that is very popular in Europe and the United States. Google lists C++, Java, and Python as its three major internal languages, and apparently many internal tools are written in Python.

There is another similar scripting language called Ruby. Its creator is Japanese, so it is especially popular in Japan.

Installing the official Python distribution is enough to get started, but it is much more convenient to also have pip, a package manager, and IPython, a shell, so I install those too. This assumes a Windows environment. For Linux, install things however you prefer.

  1. Install Python
  2. Configure environment variables
  3. Install setuptools
  4. Install pip
  5. Install ipython

1. Install Python

Download the installer that matches your environment from here and install it. At the moment there are both 2.7 and 3.3 series releases, but considering library compatibility, the 2.7 series is the safer choice.

2. Configure environment variables

If you installed Python 2.7, it should be installed under C:\Python27 by default. Right-click My Computer, open [Properties] -> [Advanced Settings tab] -> [Environment Variables], and add the following paths to PATH.

C:\Python27 C:\Python27\Scripts

Then reboot the machine.

3. Install setuptools

Download and install setuptools-0.6c11.win32-py2.7.exe (md5) from here.

4. Install pip

pip is a package management tool. If you install it, package management becomes very easy from the command line.

Download pip-1.2.1.tar.gz (md5, pgp) from here, extract it, open a command line, move into the extracted folder, and run the following command.

$ python setup.py bdist_wininst

That command creates a Windows installer for pip under the dist folder. Use the generated installer to install pip.

5. Install ipython

From the command line:

$ pip install ipython

That command installs ipython and pyreadline through pip.

Starting the shell

Open a command line, type ipython, and press Enter. If the configuration is correct, the IPython shell starts. The first time you launch it, it asks several questions, and I think answering YES to all of them was fine. After that, you can use an interactive shell. In interactive mode, results are shown immediately.

In 1: print "Hello, world!" Hello, world!

If it behaves like that, the setup is good. Interactive mode is very convenient for quick checks, but if you want to write a larger piece of logic, it is better to create a script file in a text editor.

#! /usr/bin/env python
# -*- coding: utf-8 -*-

print "Hello, world!"

If you want to write a Python main, use the following form.

#! /usr/bin/env python
# -*- coding: utf-8 -*-

if __name__ == "__main__":
print "Hello, world!"

After creating the files above, run them from the command line with:

pythonscript.pypython script.py python main.py

Once the installation is finished, I recommend first reading the official tutorial.