2011-01-17 14 views
7

Estoy tratando de incrustar Python 2.6 en .NET 4.0.Python para .NET: ImportError: no hay ningún módulo llamado advertencias

Siguiendo la documentación muy mínima en "Python para .NET", me escribió un código bastante sencillo de la siguiente manera:

const string pythonModulePath = @"C:\Projects\PythonImport\PythonImport\test.py"; 
Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(python 
ModulePath)); 

PythonEngine.Initialize(); 

var oldWorkingDirectory = Directory.GetCurrentDirectory(); 
var currWorkingDirectory = Path.GetDirectoryName(pythonModulePath); 

Directory.SetCurrentDirectory(currWorkingDirectory); 

var pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(python 
ModulePath)); 
if (pyPlugin == null) 
{ 
     throw new PythonException(); 
} 

Directory.SetCurrentDirectory(oldWorkingDirectory); 

Incluso si test.py está vacía me sale un error de importación diciendo "No hay ningún módulo llamado advertencias ".

'import site' failed; use -v for traceback 

Unhandled Exception: Python.Runtime.PythonException: ImportError : No module nam 
ed warnings 

El motivo se hace evidente cuando ejecuta test.py utilizando "python -v" (modo detallado). Observe que python llama a las advertencias #cleanup [2].

¿Por qué Python .NET no puede resolver las advertencias? El módulo está presente en el directorio Lib.

¿Alguna idea?

Respuesta

2

Creo que por defecto el motor de Python no establece las rutas que necesita automáticamente.

http://www.voidspace.org.uk/ironpython/custom_executable.shtml tiene un ejemplo para incrustar ironpython. Parece que se echa en falta algo así como

PythonEngine engine = new PythonEngine(); 
engine.AddToPath(Path.GetDirectoryName(Application.ExecutablePath)); 
engine.AddToPath(MyPathToStdLib); 

a menos que sepa dónde y si se ha instalado IronPython, prefiero encontrar todos los módulos estándar que necesito, compilarlos a un IPyStdLibDLL y luego hacer º siguiente de mi código

import clr 
clr.addReference("IPyStdLib") 
import site 
1

@SilentGhost no sé si usted cuenta de esto, pero hay que establecer la variable de entorno PYTHONPATH en lugar de la variable de PYTHONHOME. Yo también tuve el problema, establecí la variable de entorno con el viejo entorno Environment.SetVariable ("PYTHONPATH", ...) y al final todo salió bien.

Buena suerte con Python.NET.

Cuestiones relacionadas