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.
This commit is contained in:
Brian 2013-12-22 19:54:15 -07:00
parent 8a1c541b7e
commit 09124edc2b
1 changed files with 27 additions and 3 deletions

View File

@ -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<MyEvtArgs<string>>(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();