How to interact with dotNet libraries from python in VSTO (the missing tutorial)

Dec 27, 2012 at 11:55 AM


I'm a knowledge domain expert and know my functional programming in Python and get perfectly by here on day-to-day basis.

I've been asked to interact through Visual Studio 2012 with a .Net optimization engine (because 97% of the project lives on TFS), and luckily python tools for visual studio (PTVS) exists.

The friendly dotNet developers have given me all the libraries for the engine, but how would the syntax from python(x.x) be, in order to exploit the attached libraries?

There must be some simple steps to do this?


For example I know that SQLite3 is written in C, and can be accessed as:

 

>>> import sqlite3
>>> sqlite3.version
'2.6.0'

I therefore expect something similar when Python scripting in VSTO. Am I missing something?

Coordinator
Dec 27, 2012 at 5:31 PM

Your best bet may be to use IronPython, which is written in .NET. There are details/downloads at http://ironpython.net/, and you'll be interested in the documentation at http://ironpython.net/documentation/dotnet/ (which covers integration with other .NET libraries).

PTVS has full support for IronPython, so once you've installed both you can use our tools, including the Interactive Window and debuggers (we have two debuggers for IronPython, one based on Python and one based on .NET). However, you won't need PTVS installed to run a script - just IronPython.

We can only provide minimal support for IronPython, but there are other options listed at http://ironpython.net/support/

Jan 3, 2013 at 4:13 PM
Hello Zooba,
I have followed the instructions including adding references to the Application's project in MyOwnSubProject in VSTO. It didn't help.

When I do this clr works:
>>> import clr
>>> for r in clr.References: print r
...
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
IronPython.SQLite, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1
Microsoft.Scripting, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1

But when I do this, I get the following errormessage:
>>> clr.AddReference("KG.SSC.Common")
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: System.IO.IOException: Could not add reference to assembly KG.SSC.Common
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object reference)
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object[] references)
at Microsoft.Scripting.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Runtime.FunctionCaller`1.Call1(CallSite site, CodeContext context, Object func, T0 arg0)
at IronPython.Runtime.Method.MethodBinding.SelfTarget(CallSite site, CodeContext context, Object target)
at Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)

The same error message occurs when I try this:
>>> clr.AddReference('C:\\Users\\bmadsen\\Desktop\\Smart Supply Chain\\SmartSupplyChain\\Smart Supply Chain\\KG.SSC.Common\\KG.SSC.Common')
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: System.IO.FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent)
at System.Reflection.AssemblyName..ctor(String assemblyName)
at System.Reflection.Assembly.LoadWithPartialName(String partialName)
at IronPython.Runtime.ClrModule.LoadAssemblyByPartialName(String name)

Any good ideas?


On 27 December 2012 17:31, Zooba <notifications@codeplex.com> wrote:

From: Zooba

Your best bet may be to use IronPython, which is written in .NET. There are details/downloads at http://ironpython.net/, and you'll be interested in the documentation at http://ironpython.net/documentation/dotnet/ (which covers integration with other .NET libraries).

PTVS has full support for IronPython, so once you've installed both you can use our tools, including the Interactive Window and debuggers (we have two debuggers for IronPython, one based on Python and one based on .NET). However, you won't need PTVS installed to run a script - just IronPython.

We can only provide minimal support for IronPython, but there are other options listed at http://ironpython.net/support/

Read the full discussion online.

To add a post to this discussion, reply to this email (pytools@discussions.codeplex.com)

To start a new discussion for this project, email pytools@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




--
Bjorn Madsen
Researcher Complex Systems Research
Ph.: (+44) 0 7792 030 720

Coordinator
Jan 3, 2013 at 5:40 PM

This is definitely an IronPython support question, so you may get more help on one of their lists, but I'll take a guess.

>>> clr.AddReference("KG.SSC.Common")
 
This command requires the assembly to be installed in the GAC. The error message makes it look like it isn't, but it may be that adding ".dll" to the end of the name would make it load from the current directory (and/or search paths, I'm not 100% sure about the logic that IronPython uses here). There is one example on their documentation page (under Generic Methods) where a DLL in the current directory is loaded using AddReference("convert.dll").
 
>>> clr.AddReference('C:\\Users\\bmadsen\\Desktop\\Smart Supply Chain\\SmartSupplyChain\\Smart Supply Chain\\KG.SSC.Common\\KG.SSC.Common')
 
This one almost certainly needs ".dll" on the end of the name, and probably should be using clr.AddReferenceToFile() instead of plain AddReference().