2015-06-05 7 views
6

Ho un problema nel caricare una DLL esterna utilizzando Python tramite Python per .NET. Ho provato diversi metodi seguendo StackOverflow e simili. Cercherò di riassumere la situazione e di descrivere tutti i passaggi che ho fatto.Python per .NET

Ho una dll chiamata per es. Test.NET.dll. Ho controllato con dotPeek e posso vedere, facendo clic su di esso, x64 e .NET Framework v4.5. Sul mio computer ho installato .Net Framework 4.

Ho anche installato Python per .NET in diversi modi. Penso che il migliore sia scaricare il .whl da questo sito web LINK. Ho scaricato e installato: pythonnet-2.0.0.dev1-cp27-none-win_amd64.whl. Posso immaginare che funzionerà per .NET 4.0 dal Richiede Microsoft .NET Framework 4.0.

Una volta che ho installato tutto, non posso fare questi comandi:

>>> import clr 
>>> import System 
>>> print System.Environmnet.Version 
>>> print System.Environment.Version 
4.0.30319.34209 

Sembra lavoro. Poi, ho cercato di caricare la mia dll a digitare i seguenti comandi:

>>> import clr 
>>> dllpath= r'C:\Program Files\API\Test.NET' 
>>> clr.AddReference(dllpath) 

Traceback (most recent call last): 
    File "<pyshell#20>", line 1, in <module> 
    clr.AddReference(dllpath) 
FileNotFoundException: Unable to find assembly 'C:\Program Files\API\Test.NET'. 
    at Python.Runtime.CLRModule.AddReference(String name) 

Ho anche provato ad aggiungere 'dll' alla fine del percorso, ma nulla è cambiato. Poi, ho anche provato diverse soluzioni come descritto in LINK, LINK, LINK e molto altro .... Sfortunatamente, non funziona e ottengo diversi errori. So che esiste IronPython ma stavo cercando di evitare di usarlo.

Grazie per il vostro aiuto!

+0

1. hai provato con FindAssembly? 2. Sono stati analizzati gli errori dal visualizzatore del registro di binding degli assiemi (Fuslogvw.exe)? 3. il tuo assembly è a 64 bit, è anche Python a 64 bit? – denfromufa

risposta

1

Did Test.NET.dll proviene da un altro computer? Secondo il thread this, alcune funzionalità di sicurezza di .NET possono impedire il caricamento corretto di .dlls.

Per un messaggio di errore più informativo, provare

> from clr import System 
> from System import Reflection 
> full_filename = r'C:\Program Files\API\Test.NET' 
> Reflection.Assembly.LoadFile(dllpath) 

Se si ottiene un messaggio di errore lungo le linee di

NotSupportedException: An attempt was made to load an assembly from a 
network location which would have caused the assembly to be sandboxed in 
previous versions of the .NET Framework. This release of the .NET Framework 
does not enable CAS policy by default, so this load may be dangerous. If 
this load is not intended to sandbox the assembly, please enable the 
loadFromRemoteSources switch. 

allora la seguente risolto il problema per me:

  • fare clic con il tasto destro su Test.NET.dll
  • selezionare 'Proprietà'
  • sotto la scheda 'Generale', clicca su 'Sblocca'
  • clic su 'Apply'
1

Questa non è una risposta completa, ma aiutare per i lettori futuri: Si non dovrebbe fidarsi di FileNotFoundExceptionclr.AddReference(dllpath) Purtroppo mentre sondaggio assemblee eccezioni vengono ingerite

public static Assembly AddReference(string name) 
{ 
    AssemblyManager.UpdatePath(); 
    Assembly assembly = null; 
    assembly = AssemblyManager.LoadAssemblyPath(name); 
    if (assembly == null) 
    { 
     assembly = AssemblyManager.LoadAssembly(name); 
    } 
    if (assembly == null) 
    { 
     string msg = String.Format("Unable to find assembly '{0}'.", name); 
     throw new System.IO.FileNotFoundException(msg); 
    } 
    return assembly ; 
} 

AssemblyManager.LoadAssemblyPath rondini e ccezioni

try { assembly = Assembly.LoadFrom(path); } 
catch {} 

AssemblyManager.LoadAssembly rondini eccezioni troppo

try { assembly = Assembly.Load(name);} 
catch (System.Exception e) {} 

È possibile controllare l'elenco delle possibili eccezioni inghiottite in Assembly.LoadFrom e Assembly.Load per scoprire possibili vere ragioni