Setting up Python for development on a Windows environment turns out to be not such an easy task.
After setting up several such environments and running into all sort of problems I had to figure out I decided it would probably be worthwhile for myself and other developers to document the process…
If you’re starting Python development on Windows, and especially if you’re using Google App Engine (as this guide is about Python 2.5.4) this guide is for you…
Install the Python Runtime
The first step in setting up a Python development environment is to install the Python runtime.
For the purpose of this guide we’ll be installing Python version 2.5 (the latest at this time is 2.5.4) as
that’s the version required for Google App Engine development which is my main use for Python (it’s also a mature version, with wide support etc.)
Just download the msi that fits your system and install it.
Unless you chose otherwise, the installer put everything in c:\Python25
Set up your Environment
Setting the PYTHON_HOME Environment Variable
We’ll set PYTHON_HOME to c:\Python25 and add it to the PATH variable:
- Open the environment variable settings dialog – right-click "My Computer", select "Properties", "Advanced Tab" and click the "Environment Variables" button.
(Alternatively, on Windows 7 you can just type "edit the system environment variables" on the start menu)
- Under System Variables click the button New… and add a variable called PYTHON_HOME that points to c:\Python25
- Now edit the PATH system variable and add ;%PYTHON_HOME%; to its end.
You can test that you’ve done this correctly by firing up the Commend Prompt and typing "echo %PYTHON_HOME%":
Setting up the PATH variable
The PATH variable is a list of paths separated by a ";" character.
On the same dialog used before, find the PATH system variable, edit it and add ";%PYTHON_HOME%" (notice the preceding ; character).
Now you can run python from anywhere in the system.
Open a new Command Prompt window and type "python" and you should get the Python prompt:
What’s easy_install? easy_install makes it easy to fetch and install Python libraries and their dependencies.
easy_install gets installed into c:\Python25\Scripts. As we want it accessible from anywhere we’ll want to add that path to the system’s PATH environment variable.
So, edit the PATH variable again and add %PYTHON_HOME%\Scripts to its end (and don’t forget to precede it with ";")
Now easy_install is available from anywhere:
Now you can install libraries from Python’s library repository. For example you can install Nose simply by running "easy_install nose":
Support Installing C Libraries
One of the features in Python is being able to use code written in C via extensions.
Various libraries make use of this feature (especially for security and scientific calculations).
In order for the libraries distributions to remain platform natural, they’ll usually be distributed with their C source code along with their python source, and require compilation on the target machine when installed.
On Linux and other OS’s this is usually not a problem and the library will build the C extension without an issue. On Windows however, it is likely that there’s no C++ compiler on the system to do this operation.
For example, try running "easy install pycrypto" :
As you can see from the error, the C extension could not be built.
While we can use Visual Studio 2003 to do this, it is way easier (and free) to use MingW32…
When you read the "Select Components" part of the setup, make sure both the "C compiler" and "C++ Compiler" packages are selected.
Once the installation is done, add "C:\MinGW\bin" to your PATH system environment variable.
We now have a C++ compiler for easy_install to use to build C extensions and all we need to to do is tell it to use it instead of Visual Studio 2003. We do this by creating a file called "distutils.cfg" at c:\Python25\Lib\distutils containing the following:
You should now be able to install PyCrypto by running "east_install pycrypto".
You can verify the installation by running Python and trying to import it:
Install Support for SSL
As mentioned earlier, I’m mostly using Python for Google App Engine development.
To securely communicate with Google’s servers via SSL we’ll need to install SSL support.
The process for enabling SLL is as follows:
- Download the "Complete Package, except sources" of OpenSSL from here install it: http://gnuwin32.sourceforge.net/packages/openssl.htm
- Add environment variable called GNUWIN32_DIR that points to the OpenSSL installation folder (by default its C:\Program Files (x86)\GnuWin32)
- Download and unzip libgw32c-0.4-lib.zip from the libgw32c section of this page: http://sourceforge.net/projects/gnuwin32/files/ (direct link: http://sourceforge.net/projects/gnuwin32/files/libgw32c/0.4/libgw32c-0.4-lib.zip/download)
Copy the files and folders in it to the GNUWIN32_DIR folder
- Download the Python SSL Library from http://pypi.python.org/pypi/ssl/ (direct download)
and extract it to a temporary folder
- Build the SSL library by running: setup.py build -c mingw32
- Install it by running: setup.py install –skip-build
- Verify installation worked:
- None Found