Uncategorized

The Complete Guide to Setting up Python Development Environment on Windows


Eran Kampf

Eran Kampf



5 min read

(Originally adapted and updated from Tom Willis’s posts here and here). Also, as commenters suggested, you should take a look at ActiveState which can probably save you all this trouble…

images
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.)

You can find a list of all the Python releases here.
The Python 2.5.4 version is here.

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)
    SettingPython-1
  • 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%”:

image

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:

image

Installing easy_install

What’s easy_install? easy_install makes it easy to fetch and install Python libraries and their dependencies.

Download and run the setuptools installation package for Python 2.5
(when writing this guide, the version was 0.6c11)

easyinstall 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:

image

Now you can install libraries from Python’s library repository. For example you can install Nose simply by running “easy_install nose”:

image

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” :

SettingPython

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…

Install MingW

Download and install the “Automated MingW installer” from here (if the link doesn’t work just go to http://mingw.org and press the download link on the left)

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:

[build]
compiler=mingw32

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:

image
If you didn’t get an error messaging when importing Crypto then PyCrypto is installed correctly.

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:

image

Final Words

You have a Python development environment ready! Wasn’t simple, but you did it…
Now I recommend getting a decent IDE.. Try Eclipse with PyDev or JetBarin’s new PyCharm



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *