[Python.NET] Do assemblies need to be placed into the GAC

Brian Lloyd brian at zope.com
Wed Oct 29 10:38:51 EST 2003

> Thanks for that. We don't currently put our assemblies into the 
> GAC because 
> our ASP.NET app can (theoretically) support multiple versions on the one 
> server machine. Each version lives in a separate sub-directory that then 
> contains a 'bin' directory for the assembly DLLs.
> Although you *can* put multiple versions of the same component 
> into the GAC 
> (as long as the AssemblyVersion number is different), how would 
> Python.NET 
> distinguish these?

Right now it uses Assembly.LoadWithPartialName, which seems (the 
docs are vague on this) to load the most recent version of an 
assembly that it can find.

The ability to side-by-side load different versions of assemblies 
is one of the (relatively few) places where the matching of meta-models 
between Python and the CLR breaks down.

Because Python import is name-based and unaware of any concept of 
versioned modules, my goal has been for 'implicit assembly loading' 
(loading as a result of import) to do something consistent and 
reasonable (i.e. load most recent available based on name).

It is possible to load a specific version of an assembly - you would 
do something like:

  from CLR.System.Reflection import Assembly, AssemblyName

  name = AssemblyName(...) # set required version, etc.
  assembly = Assembly.Load(name)

  # now import namespaces from the loaded assembly

Things get a lot more complicated if you need to load more than one 
version of a particular assembly (or more likely, you have a dependency 
on some library the does so). In this case, the names you access via 
the CLR modules will always come from the first version of the 
assembly loaded (which will always win in the internals of the runtime).

That doesn't mean that you're hosed though - you just have to do 
more work to get the right versions of objects, a la:

  from CLR.System.Reflection import Assembly, AssemblyName
  from System import Activator

  name = AssemblyName(...) # get the right version
  assembly = Assembly.Load(name)
  type = assembly.GetType("QualifiedNameOf.TheTypeINeed")
  obj = Activator.CreateInstance(type)

Hope that helps somewhat. BTW, these are all great questions and 
I'm making notes to put these things in the faq / docs :^)

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com

More information about the PythonDotNet mailing list