Wednesday, February 13, 2008

Installing Python.NET 2.0 Alpha 2 on Windows XP

Python.NET is a project that lets you use .NET libraries from within Python [1]. The latest version (2.0) is still alpha as of this writing, so the project owners do not provide binary downloads. Even though it's alpha, I still vastly prefer version 2.0 over 1.0 because it has fewer warts [2], and its API is compatible with IronPython. In practice, it does seem pretty stable too.

I'm going to explain how I compiled and installed Python.NET 2.0 using Visual C# 2008 Express Edition, a free IDE for C#. If you don't have it installed yet, see my previous post.

Note

If you don't want to go through all the hassle of following the steps below, you can download the binary files I created here.

Here are the steps:

  1. Download the source files from Sourceforge. You can choose either a tarball or a zip file.

  2. Extract the contents to your hard drive and open the pythonnet.sln solution file using Visual C#.

  3. Convert the solution file to the Visual C# 2008 format [3].

  4. There are several projects in the solution file. The main one is called Python.Runtime. Right-click on that project and select Properties.

  5. In the Application tab, change Target Framework to ".NET Framework 2.0".

  6. The default is to build binaries for Python 2.5 and UCS2. If you need to change this, you need to make some additional tweaks [4].

  7. From the menu, select Build -> Configuration Manager. In the dialog that opens, change the Active solution configuration to Release. Press the Close button.

  8. From the menu, select Build -> Build Solution.

  9. The binary files you want can be found under src\runtime\bin\Release:

    • Python.Runtime.dll
    • clr.pyd
  10. Copy the two binary files into your Python directory (e.g. C:\Python25).

  11. Test the installation by starting the shell and typing import clr. If there are no errors, it probably worked.

If you're going to PyCon this March and are interested in learning more about Python.NET, then come to my talk! [5]

[1]Python.NET works with CPython, the default implementation of Python. It's not to be confused with IronPython, which is implemented in .NET.
[2]For example, importing System.Data is a pain if you have both .NET 1.1 and .NET 2.0 installed.
[3]Because Python.NET was originally compiled using Visual C# 2005, you will be prompted with the Visual Studio Conversion Wizard dialog. You should go ahead and convert the project.
[4]Go to the Properties -> Build tab [4]. Change the Configuration combo box to Release. Then change the value inside the Conditional compilation symbols field. For example, PYTHON24,UCS4 will build a binary for Python 2.4 with UCS4. For more details about the difference between UCS2 and UCS4, see the readme.html file inside the doc folder.
[5]According to the official schedule, my talk will be held on Friday, March 14 at 2:45 pm.
.. entry_id:: tag:blogger.com,1999:blog-5424252364534723300.post-7884767366505308962

Installing Python.NET 2.0 Alpha 2 on Windows XP
===============================================
.. labels:: python, dotnet, python.net, visual studio

Python.NET_ is a project that lets you use .NET libraries from within Python [#]_. The latest version (2.0) is still alpha as of this writing, so the project owners do not provide binary downloads. Even though it's alpha, I still vastly prefer version 2.0 over 1.0 because it has fewer warts [#]_, and its API is compatible with IronPython_. In practice, it does seem pretty stable too.

.. _Python.NET: http://pythonnet.sourceforge.net
.. _IronPython: http://codeplex.com/ironpython
.. _Visual C# 2008 Express Edition: http://www.microsoft.com/express/vcsharp/

I'm going to explain how I compiled and installed Python.NET 2.0 using `Visual C# 2008 Express Edition`_, a free IDE for C#. If you don't have it installed yet, see my `previous post`_. 

.. _previous post: http://feihonghsu.blogspot.com/2008/02/installing-visual-studio-2008-express.html

.. note::
    If you don't want to go through all the hassle of following the steps below, you can download the binary files I created here_.
    
.. _here: http://feihong.hsu.googlepages.com/PythonNET2.0.zip

Here are the steps:

#. Download the `source files from Sourceforge`_. You can choose either a tarball or a zip file.
#. Extract the contents to your hard drive and open the ``pythonnet.sln`` solution file using Visual C#. 
#. Convert the solution file to the Visual C# 2008 format [#]_.
#. There are several projects in the solution file. The main one is called Python.Runtime. Right-click on that project and select ``Properties``. 
#. In the ``Application`` tab, change ``Target Framework`` to ".NET Framework 2.0".
#. The default is to build binaries for Python 2.5 and UCS2. If you need to change this, you need to make some additional tweaks [#]_.
#. From the menu, select ``Build -> Configuration Manager``. In the dialog that opens, change the ``Active solution configuration`` to ``Release``. Press the ``Close`` button.
#. From the menu, select ``Build -> Build Solution``.
#. The binary files you want can be found under ``src\runtime\bin\Release``:

    - Python.Runtime.dll
    - clr.pyd

#. Copy the two binary files into your Python directory (e.g. ``C:\Python25``).
#. Test the installation by starting the shell and typing ``import clr``. If there are no errors, it probably worked.

.. _source files from Sourceforge: http://sourceforge.net/project/showfiles.php?group_id=162464

If you're going to PyCon_ this March and are interested in learning more about Python.NET, then come to my talk!  [#]_

.. _PyCon: http://us.pycon.org/2008

.. [#] Python.NET works with CPython_, the default implementation of Python. It's not to be confused with IronPython_, which is implemented in .NET.

.. [#] For example, importing ``System.Data`` is a pain if you have both .NET 1.1 and .NET 2.0 installed.

.. [#] Because Python.NET was originally compiled using Visual C# 2005, you will be prompted with the Visual Studio Conversion Wizard dialog. You should go ahead and convert the project.

.. [#] Go to the ``Properties -> Build`` tab [4]_. Change the ``Configuration`` combo box to ``Release``. Then change the value inside the ``Conditional compilation symbols`` field. For example, ``PYTHON24,UCS4`` will build a binary for Python 2.4 with UCS4. For more details about the difference between UCS2 and UCS4, see the ``readme.html`` file inside the ``doc`` folder.

.. [#] According to the official schedule_, my talk will be held on Friday, March 14 at 2:45 pm.

.. _CPython: http://en.wikipedia.org/wiki/Cpython
.. _schedule: http://us.pycon.org/2008/conference/schedule/

No comments: