Menu

#17 clr.AddReference fails in python script

closed
nobody
None
5
2014-08-27
2011-03-09
wallach2009
No

clr.AddReferenc fails on full assembly pathwhen called in python script, but succeeded when called directly in python shell

Log (see test.py attached):

C:\>c:\Python26\python.exe c:\test.py
Traceback (most recent call last):
File "c:\test.py", line 2, in <module>
clr.AddReference(r"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq")
System.IO.FileNotFoundException: Unable to find assembly 'c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq'.
at Python.Runtime.CLRModule.AddReference(String name) in H:\Documents\Visual Studio 2008\Projects\PySharp\trunk\pythonnet\src\runtime\moduleobject.cs:line 375

C:\&gt;c:\Python26\python.exe
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference(r"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq")
<System.Reflection.Assembly object at 0x00C97EB8>
>>>

Discussion

  • wallach2009

    wallach2009 - 2011-03-09

    sample python script

     
  • Barton Cline

    Barton Cline - 2011-03-10

    This works in the script form:
    import sys
    sys.path.append("c:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.5\\")

    import clr
    clr.AddReference("System.Xml.Linq")

     
  • Barton Cline

    Barton Cline - 2011-03-10
    • status: open --> closed
     
  • Barton Cline

    Barton Cline - 2011-03-10

    I guess that it's time to do a Framework 3.5 build which will automatically search the desired directory.

     
  • wallach2009

    wallach2009 - 2011-03-10

    Barton, thanks for an instant response and proposed solution which did solve my problem.
    I think for convenience it's worth adding extra method like clr.AddReferenceByPath(fullPath) a wrapper over Assembly.LoadFile.

     
  • wallach2009

    wallach2009 - 2011-03-10

    .. successful clr.AddReference in shell is strange:

    it succeeded when called with full path
    C:\&gt;c:\Python26\python.exe
    Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> print sys.path
    ['', 'c:\\Python26\\python26.zip', 'c:\\Python26\\DLLs', 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win', 'c:\\Python26\\lib\\lib-tk', 'c
    onwin']
    >>> import clr
    >>> a=clr.AddReference(r"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq")
    >>> a.Location
    u'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Xml.Linq\\3.5.0.0__b77a5c561934e089\\System.Xml.Linq.dll'
    >>>

    but fails on name

    C:\&gt;c:\Python26\python.exe
    Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import clr
    >>> a=clr.AddReference(r"System.Xml.Linq")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    System.IO.FileNotFoundException: Unable to find assembly 'System.Xml.Linq'.
    at Python.Runtime.CLRModule.AddReference(String name) in H:\Documents\Visual Studio 2008\Projects\PySharp\trunk\pythonnet\src\runtime\mod
    >>>

     
  • Barton Cline

    Barton Cline - 2011-03-13

    Re-opening: I've built referencing the 3.5 framework. System.Xml.Linq (not to mention System.Core) is in the GAC. None of them are being found. Interestingly:

    import sys
    import clr
    sys.path.append("c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.5/")
    for d in sys.path:
    print d
    print "================================="
    print

    from System.Runtime.InteropServices import RuntimeEnvironment
    print RuntimeEnvironment.GetRuntimeDirectory()
    sysAssy = clr.AddReference("System.Core")
    print sysAssy.FullName
    print RuntimeEnvironment.FromGlobalAccessCache(sysAssy)

    C:\Users\Barton\Documents\Python>python test.py
    C:\Users\Barton\Documents\Python
    C:\Windows\system32\python26.zip
    C:\Python26\DLLs
    C:\Python26\lib
    C:\Python26\lib\plat-win
    C:\Python26\lib\lib-tk
    C:\Python26
    C:\Python26\lib\site-packages
    C:\Python26\lib\site-packages\wx-2.8-msw-ansi
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\ c:/Program Files/Reference Assemblies/Microsoft/Framework/v3.5/
    =================================

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\ System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    True

     
  • Barton Cline

    Barton Cline - 2011-03-13
    • status: closed --> open
     
  • Barton Cline

    Barton Cline - 2011-03-13
    • status: open --> closed
     
  • Barton Cline

    Barton Cline - 2011-03-13

    Closed: Not (technically) a clr bug).
    Due to the way the Assembly.Load(string) searches, it is necessary to give the full name of the assembly that may match the name of a pre-loaded assembly (System, in this case):

    import clr

    from System.Runtime.InteropServices import RuntimeEnvironment
    print RuntimeEnvironment.GetRuntimeDirectory()
    sysAssy = clr.AddReference("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    print sysAssy.FullName
    print RuntimeEnvironment.FromGlobalAccessCache(sysAssy)

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\ System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    True

     
  • Barton Cline

    Barton Cline - 2011-03-16

    On Ubuntu 10.10 - the Maverick Meerkat things work as expected:
    # Python 2.6.6 (r266:84292, Sep 15 2010, 16:02:57)
    [GCC 4.4.5]

    <script>
    import clr
    sysCore = clr.AddReference("System.Core")
    sysAssy = clr.AddReference("System")
    print sysAssy.Location
    print sysCore.Location
    </script>
    /opt/mono-2.8/lib/mono/gac/System/2.0.0.0__b77a5c561934e089/System.dll
    /opt/mono-2.8/lib/mono/gac/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll

    So now somebody needs to check the CLR specification to see if M$ has screwed this one up...

     
  • Nobody/Anonymous

    7fyAYl <a href="http://sqatgmiehpjy.com/">sqatgmiehpjy</a>, [url=http://wdksaidijupd.com/]wdksaidijupd[/url], [link=http://aeagfoukrbci.com/]aeagfoukrbci[/link], http://pvqxienizelk.com/

     
  • Nobody/Anonymous

    kjD1h4 <a href="http://mtdgehjrordp.com/">mtdgehjrordp</a>, [url=http://sktekmucboui.com/]sktekmucboui[/url], [link=http://opizmmdkbhwc.com/]opizmmdkbhwc[/link], http://gzsnvxrfnsvz.com/

     
  • Nobody/Anonymous

    iDbbaX <a href="http://ecowydphimir.com/">ecowydphimir</a>, [url=http://ubxmhlrayupv.com/]ubxmhlrayupv[/url], [link=http://wfsupeijggru.com/]wfsupeijggru[/link], http://xvrxuwhwhfyv.com/

     
  • Nobody/Anonymous

    ZWVEk8 <a href="http://yxggoqdeglxy.com/">yxggoqdeglxy</a>, [url=http://mpnsmmfrhybd.com/]mpnsmmfrhybd[/url], [link=http://sgltiabnfqjz.com/]sgltiabnfqjz[/link], http://gxkojnonglbw.com/

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.