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:\>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>
>>>
sample python script
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")
I guess that it's time to do a Framework 3.5 build which will automatically search the desired directory.
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.
.. successful clr.AddReference in shell is strange:
it succeeded when called with full path
C:\>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:\>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
>>>
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
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
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...
7fyAYl <a href="http://sqatgmiehpjy.com/">sqatgmiehpjy</a>, [url=http://wdksaidijupd.com/]wdksaidijupd[/url], [link=http://aeagfoukrbci.com/]aeagfoukrbci[/link], http://pvqxienizelk.com/
kjD1h4 <a href="http://mtdgehjrordp.com/">mtdgehjrordp</a>, [url=http://sktekmucboui.com/]sktekmucboui[/url], [link=http://opizmmdkbhwc.com/]opizmmdkbhwc[/link], http://gzsnvxrfnsvz.com/
iDbbaX <a href="http://ecowydphimir.com/">ecowydphimir</a>, [url=http://ubxmhlrayupv.com/]ubxmhlrayupv[/url], [link=http://wfsupeijggru.com/]wfsupeijggru[/link], http://xvrxuwhwhfyv.com/
ZWVEk8 <a href="http://yxggoqdeglxy.com/">yxggoqdeglxy</a>, [url=http://mpnsmmfrhybd.com/]mpnsmmfrhybd[/url], [link=http://sgltiabnfqjz.com/]sgltiabnfqjz[/link], http://gxkojnonglbw.com/