[Python.NET] exposing modules

Matthew Pontefract MP at credaris.com
Thu Jun 15 12:12:05 CEST 2006

Hi Srivatsa

I am working in a group building a C# application and have been bringing
my Python background to the project so I have worked with both
IronPython and, most recently, PythonNet. 

Python .NET uses the normal CPython and provides something of an
interface layer that allows your Python code to call, use and interact
with .NET assemblies. As you have found, you can embed it in .NET code
and call from .NET into it.  It is a bridge solution that provides a
path between CPython and .NET, but the Python code is NOT compiled to
MSIL, so your python classes will not be accessible to .NET code

IronPython is a first class .NET language. Python code is compiled to
MSIL and executed in the .NET machine. Python classes can interact much
more closely with .NET code and it is possible I believe (though I've
not done this) to create .NET assemblies and uses classes coded in
Python used by any other .NET application. It's Python for .NET in the
proper sense. It's easy to embed and put a Python console into your .NET

The downside of IP is that it is a relatively young product and you
can't expect just any Python library to run in IronPython because of
differences in provision of a number of core packages, including OS and
a number of others. It is fantastic for adding scripting to a .NET
application, but it is less useful (at the moment - this will change I
have no doubt) for running scripts that leverage many of the more
complex Python packages out there. On the flip side, being a .NET
language, you can debug into your IronPython run code using dev studio.
PyUnit may or may not work on IronPython (probably will, actually, but
I've not tried).

PythonNet is ideal for me because I don't need my C# code to call Python
objects - but I need the Python to work with my .NET assemblies and I
want to use a number of CPython packages. 

PyUnit is the Python unit testing framework. As I said, I'm not sure how
well it fares under IronPython, but it is simple enough to use that it's
worth a try; it'll only take up half an hour or so I'm sure. If you wish
simply to write testing of your .NET code in Python, I would have
thought that PythonNet will do the trick just fine. You needn't call it
from within a .NET class either, unless that is a necessity for some
reason. You can just run it from the command line. 

By installing PythonNET into your current installation you can run
normal python from the command line, import clr, and then work with .NET
objects. By adding the path to your .NET dlls you can access all your
code and, of course, run tests on it. If you wish only to run unit
tests, this is probably the approach that I would take. You can use
PyDev on Eclipse etc to debug through code if needed.

I have recently built a version from Brian's subversion sources to run
under .NET 2 and installed it such that I can use it from CPython. I can
help with this if you'd like to go down this route in .NET 2. If you're
using .NET 1 then the binaries are available all ready packaged and you
shouldn't need to do any compilation.


-----Original Message-----
From: Srivatsa, Radhika [mailto:Radhika.Srivatsa at ngc.com] 
Sent: 14 June 2006 14:47
To: Matthew Pontefract
Subject: RE: [Python.NET] exposing modules

Thank you very much for your inputs.

After reading more and more about Python and its different flavors, I
think I have a more fundamental question.

Here's what I am trying to achieve and could you pls let me know what is
the best tool I can use for doing this:

Currently, I have a .NET project (solution) application running on
This application is written in VC++ and we are now using Visual Basic to
unit test the code and my goal is to replace VB by Python such that I
can write Python scripts to achieve the Unit Testing. I know Python will
help me eliminate the usage of VB, right ?

Now that we have identified Python, what kind do I use - PyUnit, Python
for .NET or IronPython ? It is here that I am sort of confused. 

I'd appreciate your inputs in helping me identify what kind of Python
I'd have to use to achieve my goal.


Radhika Srivatsa
UMS Software Development
Voice: (858)618-2235

>-----Original Message-----
>From: pythondotnet-bounces at python.org 
>[mailto:pythondotnet-bounces at python.org] On Behalf Of Matthew 
>Sent: Wednesday, June 14, 2006 12:25 AM
>To: Srivatsa, Radhika; Haran Shivanan; pythondotnet at python.org
>Subject: Re: [Python.NET] exposing modules
>Hi Haran
>It sounds almost as though the Python.Runtime.dll, though 
>added to your project, is not listed as a reference for the 
>code you're compiling. In Visual Studio 2005 (the only one of 
>which I have experience) you'd click References and add a new 
>one (navigating to the dll). If using nant, the <csc> element 
>needs a child element <references> containing an <include 
>Once done, and assuming you have not omitted the using 
>statement in your code (and not mis-spelled the package name) 
>I would hope your code would compile.
>I appologise if I'm merely stating the obvious here - your 
>email didn't have a huge amount to go on so I'm just running 
>down the list of things I personally would check first! If you 
>still have trouble, let us know how you are building your code 
>etc. and I'll try and help further :)
>Kind regards
>-----Original Message-----
>From: pythondotnet-bounces at python.org
>[mailto:pythondotnet-bounces at python.org] On Behalf Of Srivatsa, Radhika
>Sent: 13 June 2006 21:48
>To: Haran Shivanan; pythondotnet at python.org
>Subject: Re: [Python.NET] exposing modules
>I am trying to achieve the first step still - of emedding the 
>python interpreter within my .NET application and am kind of 
>stuck there. 
>Here's what I did:
>1) Added "Python.Runtime.dll" to the build (of .NET project).
>2) Added the following line in the constructor:
>   PythonEngine.Initialize();
>This gives me the following errors:
>error C2065: 'PythonEngine' : undeclared identifier error 
>C2228: left of '.Initialize' must have class/struct/union type
>        type is ''unknown-type''
>What other step have I missed ?
>Radhika Srivatsa
>UMS Software Development
>Voice: (858)618-2235
>>-----Original Message-----
>>From: pythondotnet-bounces at python.org
>>[mailto:pythondotnet-bounces at python.org] On Behalf Of Haran Shivanan
>>Sent: Thursday, June 08, 2006 8:27 PM
>>To: pythondotnet at python.org
>>Subject: [Python.NET] exposing modules
>>I'm trying to embed the python interpreter in my .NET-based program.
>>The basic stuff is working fine.
>>But now, I want to expose some of my program's internal data to the 
>>(ie, not just functions that live in an assembly, but access 
>to my live 
>>program's state) For example, if I have a WinForms 
>application, I want 
>>to be able to access a textbox's current text from python.
>>Ideally, I should be able to do something like this from a script:
>>import MyApp
>>s = MyApp.GetText()
>>Is there any way to do this in Python.NET?
>>And if not, can someone provide me with some pointers on how to go 
>>about adding the functionality to the Python.NET code?
>>(I believe we have to use issue a call to Py_InitModule or something,
>>Python.NET mailing list - PythonDotNet at python.org 
>Python.NET mailing list - PythonDotNet at python.org 
>Python.NET mailing list - PythonDotNet at python.org 

More information about the PythonDotNet mailing list