Sunday, January 27, 2013

Setting up Python on Windows

I don't develop on a Windows machine myself, but while volunteering at Mozilla, a number of prospective contributors have asked me for help setting up Python under Windows. If anyone has differing instructions for Windows versions not mentioned here, I would be happy to include them.

Install Python

Please find out what version of Python is used by the project you plan to work on. If you're starting a brand-new project, you probably want to install 2.7.3, unless you already know you need 3.x. (Many Mozilla projects are still using 2.6.) The version number in the following instructions will be referred to as "XX".
  1. Download a Windows installer from http://python.org/download/
  2. Run Install (take note of the install location. the default location is C:\PythonXX)

Update your Path

If you are installing multiple versions of Python, be aware that the one that appears first on the Path is the one that will be used when calling "python.exe". If you need a specific version of python for your project, either re-arrange the path or call it with "python27.exe".

Windows XP

  1. Start Menu > Settings > Control Panel > System
  2. Advanced (tab) > Environment Variables (button near bottom)
  3. In the System variables list, find the entry for Path and click Edit

Windows 7

  1. Start Menu > Control Panel > System
  2. Advanced Settings (left pane)
  3. Environment Variables (button near bottom)

Both / All Versions

  1. Find 'Path' in the system variables and click Edit.
  2. Scroll to the end of the value, add a semicolon as a separator, and add "C:\PythonXX" (or the other place you installed python to), another semicolon, and "C:\PythonXX\Scripts".

Open a DOS / cmd window for entering command line instructions

On all Windows versions, this can be done from Start Menu->Run "cmd.exe". The default prompt in a command window is "C:\WINDOWS>". This will be used in instructions to specify that the command should be run in this window.

Install setuptools

The easy_install script is a part of setuptools, and it is used to install other packages from online sources.
  1. Download ez_setup.py from http://peak.telecommunity.com/dist/ez_setup.py by visiting the address in your browser and doing 'Save As' on the file. The Desktop is a reasonable place to save it to, you won't need it long.
  2. C:\WINDOWS>python.exe c:\Users\<username>\Desktop\ez_setup.py (replace the path if you saved it in a different location).

Install pip

pip is a 'second generation' wrapper for easy_install which is more flexible and has more options. It will automatically put files in the C:\PythonXX path hierarchy so that python.exe will be able to find them.

  C:\WINDOWS>easy_install.exe pip

Source Control

If you're working on an existing project, find out what kind of source / version control system it's using. Some common source control systems (with their Windows applications) include git / GitHub, subversion / tortiseSVN, and mercurial / tortiseHG. Find out if you should 'fork' (make a personal copy of) the repository before you download / check out / clone it.

Install virtualenv

If there is any chance you are going to be developing multiple Python applications on this machine, you should install virtualenv. It allows you to install differing versions of the same library for different projects.

  C:\WINDOWS>pip.exe install virtualenv

Possible Next Steps

If you installed virtualenv in the previous step, then you want to create and activate a virtual (python) environment. It is not generally accepted to check virtual environments into source control, so be sure to set it up in a location outside of your repository. I recommend putting them in a folder called 'environments' in your home directory (c:\User\<username>).

  C:\WINDOWS>mkdir c:\User\<username>\environments
  C:\WINDOWS>virtualenv.exe c:\User\<username>\environments\<project name>
  C:\WINDOWS>source c:\User\<username>\environments\<project name>\bin\activate

From here what you do is going to depend on your project. If it is an existing project and there is a "setup.py" file in the main directory, then you probably want to:

    C:\WINDOWS>python.exe setup.py develop

If there is no setup.py file but there is a requirements.txt file, then:

    C:\WINDOWS>pip.exe install -Ur requirements.txt

If neither of these apply to your project, consult with its documentation to find out how to install the project's pre-reqs. 

1 comment:

  1. Immediately after posting this guide, my RSS feed brought me an article that referenced http://www.blog.pythonlibrary.org/2011/11/24/python-101-setting-up-python-on-windows.

    ReplyDelete