[IronPython] How to Use site.py

Dino Viehland dinov at exchange.microsoft.com
Tue Sep 5 18:40:38 CEST 2006

Here's what the IronPythonConsole does to import site, this should basically work for you too.  Basically it looks like you're on the right path but your site.py needs to exist somewhere on your current sys.path which you haven't configured.

            string site = System.Reflection.Assembly.GetExecutingAssembly().Location;   // get location of ipy.exe
            site = Path.Combine(Path.GetDirectoryName(site), "Lib");                            // + Lib
            CurrentPythonEngine.AddToPath(site);                                                        // add that to the path
            try {
                CurrentPythonEngine.Import("site");                                                     // import site (any changes to sys should show up)
            } catch (Exception e) {
                MyConsole.Write(CurrentPythonEngine.FormatException(e), Style.Error);   // handle errors from site

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jesse Wiles
Sent: Sunday, September 03, 2006 11:17 AM
To: Discussion of IronPython
Subject: [IronPython] How to Use site.py


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);


      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

Jesse Wiles
users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list