I am not able to get compiled version of the StdLib to work.
I created an empty visual studio project (called StdLib) and using NUGET loaded with the latest (v2.7.3)IronPython.StdLib which creates a Lib folder in my project with all of the .py scripts. The build action of these files are set to “content” by default but tried compiling both with that setting and “embedded resource”.
in my py script I have:
import clr
clr.AddReference("StdLib")
import os
it finds the Assembly but throws “os module not found” error.
if I package the standard library files as just loose files in the build folder and do the following it works.
var runtime = Python.CreateRuntime();
var engine = Python.GetEngine(runtime);
engine.SetSearchPaths(new string[] { Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + @"\Lib" });
////load the script
dynamic script = runtime.UseFile(ParserScriptPath);
//execute the script
var result = script.main("result");
My question is
-
can you compile the standard library into a .Net Assembly through Visual Studio?
-
If so what should the build action be for the .py code files in the Lib folder?
-
Then what is the proper method to reference the Assembly so as not to fail when trying to import standard library modules?
I have reviewed the following post: IronPython: EXE compiled using pyc.py cannot import module "os"
but don’t actually have the IronPython installed and not quite sure if that’s the way to go about it. I used NUGET to add both the IronPython and IronPython.StdLib directly to my project. I assumed because I can add the IronPython.StdLib directly to my project via NUGET I could just compile it and reference it.
No, you won’t be able to compile it through Visual Studio (yet). If you want to, you’ll have to download IronPython and use pyc.py to do it from a command line.
Eventually I’ll have NuGet packages for pre-zipped and pre-compiled version of the stdlib, but I just haven’t got to it yet.