IronPython cannot import os module

my c # code:

//Create the ScriptRuntime engine = Python.CreateEngine(); //Create the scope for the ScriptEngine scope = engine.CreateScope(); string pyfile = "D:\\MyAddin\\test.py"; ScriptSource source = engine.CreateScriptSourceFromFile(pyfile); var rt = source.Execute(scope); 

and my test.py:

 import os import sys ... print("test") ... 

I have no problems at build time, but at VS runtime give me the error "cannot import os module". Where is the mistake?

+4
source share
1 answer

When placing IronPython in code, you need to add libraries to your path. Not all will be enabled by default.

You can add it through the engine:

 var engine = Python.CreateEngine(); var paths = engine.GetSearchPaths(); paths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib"); //paths.Add(@"C:\Python27\Lib"); // or you can add the CPython libs instead engine.SetSearchPaths(paths); 

Or through a script:

 import sys sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib') import os 
+5
source

Source: https://habr.com/ru/post/1494784/


All Articles