【发布时间】:2010-03-04 20:11:31
【问题描述】:
我有一个 Python 文件,内容为:
import re
import urllib
class A(object):
def __init__(self, x):
self.x = x
def getVal(self):
return self.x
def __str__(self):
return "instance of A with value '%s'" % (self.getVal())
我还有一个简单的 C# 控制台项目,代码如下:
engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("test.py");
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope); // class object created
object klaz = scope.GetVariable("A"); // get the class object
object instance = op.Call(klaz, "blabla waarde"); // create the instance
object method = op.GetMember(instance, "getVal"); // get a method
string result = (string)op.Call(method); // call method and get result (9)
Console.WriteLine("Result: " + result); //output: 'Result: blabla waarde'
(我从this * querstion and answer得到这个)
如果我在 Python 文件中省略了 import urllib 语句,一切正常。 (意味着它找到了 re 模块)
但只要我添加import urllib 或import urllib2,我就会得到以下异常:
ImportException was unhandled
No module named urllib
所以不知何故它找不到 urllib。我检查了 IronPython lib 文件夹,并且 urllib 和 urllib 2 都在那里。
当我在 C# 代码中导入 urllib 时,会引发相同的异常。 (engine.ImportModule("urllib");)
有什么想法吗?
我想在 python 代码中而不是在 C# 代码中管理导入。
(所以我想避免这样的事情:engine.ImportModule("urllib");)
编辑:
关于我实际使用它的一些额外信息(也许有人有替代方案):
我将有一个主 C# 应用程序,python 脚本将用作主应用程序的扩展或插件。
我正在使用 Python,因此我不需要编译任何插件。
【问题讨论】:
标签: c# ironpython visual-studio-2010