[Ironpython-users] Assembly references: file does not exist?

Grinfeld Wolfgang (KVYS 1) wolfgang.grinfeld at credit-suisse.com
Thu Jun 30 08:56:08 CEST 2011


Hi Dave,
 
I am not sure what the underlying problem is, but it may be better to start in C# and call Python from this environment, if you are going to mix languages.
Following works in my Silverlight app, so it will need small changes to get it working in .net 4.0.
Loading the dlls this way worked better for me than loading them in Python directly for some reason.
 
        public class ScriptData
        {
            public ScriptRuntimeSetup setup { get; set; }
            public ScriptRuntime runtime { get; set; }
            public ScriptEngine pe { get; set; }
            public ScriptScope scope { get; set; }
            public dynamic codeBehindPage { get; set; }
 
            internal ScriptData(string code, string xaml, DynamicPageContentLoader dynamicPageContentLoaderInstance)
            {
                setup = Python.CreateRuntimeSetup(null);
                setup.HostType = typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost);
                setup.Options["SearchPaths"] = new string[] { string.Empty };
                runtime = new ScriptRuntime(setup);
                pe = Python.GetEngine(runtime);
                // load platform assemblies so you don't need to call LoadAssembly in script code.
                foreach (string name in new string[] { "mscorlib"
                                                 , "System"
                                                 , "System.Windows"
                                                 , "System.Windows.Browser"
                                                 , "System.Net" 
                                                 , "Microsoft.Scripting" 
                                                 , "Microsoft.Scripting.Silverlight" 
                                                 })
                {
                    runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly(name));
                }
                runtime.LoadAssembly(typeof(Page).Assembly);
                runtime.LoadAssembly(typeof(VisualTreeHelper).Assembly);
                runtime.LoadAssembly(typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost).Assembly);
 
                pe.ImportModule("clrtype");
                pe.ImportModule("pyevent");
 
                scope = pe.CreateScope();
                ScriptSource codeSource = pe.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
                codeSource.Execute(scope);
                dynamic start = scope.GetVariable("start");
                start(xaml, this, dynamicPageContentLoaderInstance);
            }
        }

 


________________________________

From: ironpython-users-bounces+wolfgang.grinfeld=credit-suisse.com at python.org [mailto:ironpython-users-bounces+wolfgang.grinfeld=credit-suisse.com at python.org] On Behalf Of Dave Peterson
Sent: Mittwoch, 29. Juni 2011 16:32
To: Markus Schaber; ironpython-users at python.org
Subject: Re: [Ironpython-users] Assembly references: file does not exist?



Hi Markus,

 

ILSpy loads my assembly dll just fine.  As does a C# app.   Only ipy.exe complains about an error.

 

I think the missing file error is bogus and something else is going on.   But I'm not sure what.   Hopefully the below code and output will explain the situation better than English verbiage.  Note that I verify the file actually exists very earlier in the script!

 

-- Dave

 

 

IN test.py:

 

#!/bin/python

 

import clr

import os

import sys

 

FULLPATH = r"D:\path\to\my\Assembley.Net.dll"

if os.path.exists(FULLPATH):

    pathDir = os.path.dirname(FULLPATH)

    pathBase = os.path.basename(FULLPATH)

 

    # First load all assemblies ours depends on.

    for dep in ["mscorlib", "Microsoft.VisualC", "System", "System.Windows.Forms", "System.Data", ]:

        clr.AddReference(dep)

        print 'After %s: clr.References=%s' % (dep, clr.References)

    

    # Now try our assembly

    try:

        print 'Trying AddReferenceToFileAndPath(%s)' % FULLPATH

        clr.AddReferenceToFileAndPath(FULLPATH)

        print '\tWORKED! clr.References=%s' % clr.References

    except IOError as e:

        print '\tFAILED!  Exception=%s' % e

    print ''

        

    try:

        print 'Trying AddReference(%s)' % pathBase

        clr.AddReference(pathBase)

        print '\tWORKED! clr.References=%s' % clr.References

    except IOError as e:

        print '\tFAILED!  Exception=%s' % e

    print ''

    

    # Now try with the dir to the assembly DLL on sys.path

    sys.path.append(pathDir)

    try:

        print 'Trying AddReferenceToFileAndPath(%s)' % FULLPATH

        clr.AddReferenceToFileAndPath(FULLPATH)

        print '\tWORKED! clr.References=%s' % clr.References

    except IOError as e:

        print '\tFAILED!  Exception=%s' % e

    print ''

        

    try:

        print 'Trying AddReference(%s)' % pathBase

        clr.AddReference(pathBase)

        print '\tWORKED! clr.References=%s' % clr.References

    except IOError as e:

        print '\tFAILED!  Exception=%s' % e

    print ''

 

else:

    print '%s does not exist' % FULLPATH

 

 

SAMPLE OUTPUT:

 

D:\src\local>ipy test.py

 

After mscorlib: clr.References=(<mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1>)

 

After Microsoft.VisualC: clr.References=(<mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1>,

<Microsoft.VisualC, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a>)

 

After System: clr.References=(<mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1>,

<Microsoft.VisualC, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a>)

 

After System.Windows.Forms: clr.References=(<mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

>,

<System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1>,

<Microsoft.VisualC, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a>,

<System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>)

 

After System.Data: clr.References=(<mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1>,

<Microsoft.VisualC, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a>,

<System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>,

<System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089>)

 

Trying AddReferenceToFileAndPath(D:\path\to\my\Assembley.Net.dll)

        FAILED!  Exception=System.IO.IOException: file does not exist: D:\path\to\my\Assembley.Net.dll

   at IronPython.Runtime.ClrModule.AddReferenceToFileAndPath(CodeContext context, String file)

   at IronPython.Runtime.ClrModule.AddReferenceToFileAndPath(CodeContext context, String[] files)

   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)

 

Trying AddReference(Assembly.Net.dll)

        FAILED!  Exception=System.IO.IOException: Could not add reference to assembly Assembly.Net.dll

   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)

 

Trying AddReferenceToFileAndPath(D:\path\to\my\Assembley.Net.dll)

        FAILED!  Exception=System.IO.IOException: file does not exist: D:\path\to\my\Assembley.Net.dll

   at IronPython.Runtime.ClrModule.AddReferenceToFileAndPath(CodeContext context, String file)

   at IronPython.Runtime.ClrModule.AddReferenceToFileAndPath(CodeContext context, String[] files)

   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)

 

Trying AddReference(Assembly.Net.dll)

        FAILED!  Exception=System.IO.IOException: Could not add reference to assembly Assembly.Net.dll

   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)

 

 

D:\src\local>

 

 

-- Dave

 

From: Markus Schaber [mailto:m.schaber at 3s-software.com] 
Sent: Wednesday, June 29, 2011 2:30 PM
To: Dave Peterson; ironpython-users at python.org
Subject: AW: [Ironpython-users] Assembly references: file does not exist?

 

Hi, Dave,

 

Do you want to say that ILSpy won't load your assembly.dll, or it won't load MSVCR80.dll? The latter one is a native DLL, not an assembly, and thus cannot be loaded by ILSpy.

 

I re-read the thread, and my current Guess is that the "AddReference" is unable to find your "assembly.dll".

 

Did you specify the assembly name correctly? (Case? Without the ".dll" suffix?)

 

Grüße,

Markus

[Context cut to protect the innocent]

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110630/84d0b266/attachment.html>


More information about the Ironpython-users mailing list