7

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 get no problem at build time, but at runtime VS give me a error "cannot import module "os"". Where is the error?

1 Answer 1

8

When hosting IronPython in your code, you'll need to add the libraries to your path. Not all will be included 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 your script:

import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import os
Sign up to request clarification or add additional context in comments.

1 Comment

but python scripts will be installed in python path right ? not in iron python installation path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.