IronPython无法导入模块"os". [英] IronPython cannot import module "os"

查看:142
本文介绍了IronPython无法导入模块"os".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#代码:

//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);

和我的test.py:

and my test.py:

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

在构建时我没有问题,但是在运行时VS给我一个错误无法导入模块"os"".错误在哪里?

I get no problem at build time, but at runtime VS give me a error "cannot import module "os"". Where is the error?

推荐答案

在代码中托管IronPython时,需要将这些库添加到路径中.默认情况下,并非所有内容都包含在内.

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);

或通过您的脚本:

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

这篇关于IronPython无法导入模块"os".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆