[IronPython] How to Use site.py

Jesse Wiles jwiles at webworks.com
Sun Sep 3 11:17:01 PDT 2006


Hi,

Is there an example of how to sourcing site.py when instantiating a 
PythonEngine from a C# assembly can be used to add the standard CPython 
Lib path to that instances sys.path?  Here's my C# method:

    private void ExecutePythonFile(string ParamScriptPath, 
                                   IDictionary<string, object> ParamGlobals)
    {
      EngineOptions VarEngineOptions = new EngineOptions();
      VarEngineOptions.ClrDebuggingEnabled = true;
      VarEngineOptions.ExceptionDetail = true;
      VarEngineOptions.ShowClrExceptions = true;

      PythonEngine VarEngine = new PythonEngine(VarEngineOptions);

      VarEngine.Import("site");

      string VarScriptsDir = Path.GetDirectoryName(ParamScriptPath);

      ParamGlobals["ParameterScriptsDirectory"] = this.mScriptsDirectory;
      ParamGlobals["ParameterConfigurationPath"] = Path.Combine(VarScriptsDir, "configuration.xml");

      EngineModule VarMainModule = VarEngine.CreateModule("__main__", ParamGlobals, true);

      VarEngine.ExecuteFile(ParamScriptPath, VarMainModule);
    }

and here's the contents of my Lib/site.py:

import sys
sys.path.append(r'C:\Python242\Lib')

Thanks,
Jesse Wiles


More information about the users mailing list