From 09124edc2b318c90a3b83d004d569bd730f5c951 Mon Sep 17 00:00:00 2001 From: Brian Date: Sun, 22 Dec 2013 19:54:15 -0700 Subject: [PATCH] Trying to fix annoying Import Exceptions (which actually don't prevent the program or scripts from working) IronPython throws Import Exceptions for every single import, even though it imports them and the script works fine. It's annoying while trying to debug, and I'm sure it hurts the scripts performance while importing things. Issue is still not fixed, but these were things I was trying that I'm making sure to backup. And they can't hurt. --- code/src/Form1.cs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/code/src/Form1.cs b/code/src/Form1.cs index 5c5abbd..9e71010 100644 --- a/code/src/Form1.cs +++ b/code/src/Form1.cs @@ -69,6 +69,17 @@ namespace FileWatcher private Form1() { pyEngine = Python.CreateEngine(); + + // Adding these search paths, besides Lib, fixed IronPython import exceptions. Code may run a little faster now. + var paths = pyEngine.GetSearchPaths(); + paths.Clear(); + paths.Add(FileGroupTab.GetPythonDir()); + paths.Add(Path.Combine(FileGroupTab.GetPythonDir(), "DLLs")); + paths.Add(Path.Combine(FileGroupTab.GetPythonDir(), "Lib")); + paths.Add(Path.Combine(FileGroupTab.GetPythonDir(), "Lib", "site-packages")); + paths.Add(Path.GetDirectoryName(GetType().Assembly.Location)); + pyEngine.SetSearchPaths(paths); + pyEngine.Runtime.LoadAssembly(Assembly.GetAssembly(typeof(FileWatcher.Form1))); pyScope = pyEngine.CreateScope(); @@ -81,19 +92,32 @@ namespace FileWatcher m_ErrorStream.StringWritten += new EventHandler>(script_ErrorStringWritten); pyEngine.Runtime.IO.SetErrorOutput(m_MemoryStream, m_ErrorStream); - string pythonDir = FileGroupTab.GetPythonDir(); + /*string pythonDir = FileGroupTab.GetPythonDir(); if (pythonDir != null) { string pythonLibs = Path.Combine(pythonDir, "Lib"); if (Directory.Exists(pythonLibs)) { - string import = "import sys" + Environment.NewLine + "sys.path.append('" + pythonLibs + "')" + Environment.NewLine; + //string import = "import sys" + Environment.NewLine + "sys.path.append('" + pythonLibs + "')" + Environment.NewLine; + //string import = "import clr" + Environment.NewLine; + + //import += "clr.AddReference('StdLib')" + Environment.NewLine; + + import += "clr.AddReference('IronPython')" + Environment.NewLine; + import += "clr.AddReference('IronPython.Modules')" + Environment.NewLine; + import += "clr.AddReference('Microsoft.Scripting.Metadata')" + Environment.NewLine; + import += "clr.AddReference('Microsoft.Scripting')" + Environment.NewLine; + import += "clr.AddReference('Microsoft.Dynamic')" + Environment.NewLine; + import += "clr.AddReference('mscorlib')" + Environment.NewLine; + import += "clr.AddReference('System')" + Environment.NewLine; + import += "clr.AddReference('System.Data')" + Environment.NewLine; + ScriptSource source = pyEngine.CreateScriptSourceFromString(import, SourceCodeKind.AutoDetect); object result = source.Execute(pyScope); } - } + }*/ InitializeComponent();