上的urllib导入错误 - 在C#应用程序中嵌入IronPython的 [英] Embedding IronPython in a C# application - import error on urllib

查看:431
本文介绍了上的urllib导入错误 - 在C#应用程序中嵌入IronPython的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有作为内容Python文件:

I have a Python file with as content:

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#控制台项目:

I also have a simple C# console project with the following code:

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'



(我得到这个从的这个计算器querstion并回答)结果
。如果我离开了进口的urllib声明在Python文件,一切工作正常。 (这意味着它找到的re模块)结果
但只要我要么添加进口的urllib 进口的urllib2 我得到了以下异常:

(I got this from this stackoverflow querstion and answer)
If I leave out the the import urllib statement in the Python file everything works fine. (meaning it finds the re module)
But as soon as i either add import urllib or import urllib2 I get the following exception:

ImportException was unhandled
No module named urllib

所以不知它无法找到的urllib。我查了IronPython的lib文件夹和两个urllib而urllib的2是肯定有。当我在C#代码导入的urllib
同样的例外获取引发。 ( engine.ImportModule(的urllib); )?

So somehow it can't find the urllib. I checked the IronPython lib folder and both urllib and urllib 2 are definitely there. The same exception gets thrown when I import urllib in the C# code. (engine.ImportModule("urllib");)

任何想法,结果
我。倒是要管理的进口Python代码,而不是在C#代码结果
(所以我想避免这样的东西: engine.ImportModule(的urllib );

编辑:
什么,我真的要使用一些额外的信息本作(也许有人有一个二选一):结果
我将有一个主要的C#应用​​程序和Python脚本将用作扩展或插件的主要应用 -
我。使用Python这样我就不需要编译任何插件。

Some extra info on what I'm actually going to use this for (maybe someone has an alternative):
I will have a main C# application and the python scripts will be used as extensions or plugins for the main application.
I'm using Python so that I don't need to compile any of the plugins.

推荐答案

我面临同样的问题。继汤姆E公司的评论fuzzyman的答复建议,我可以成功地解决问题。这个问题似乎是它不能解析urllib.py的位置。我们需要设置它。

I face the same problem. Following "Tom E's" suggestion in the comments to fuzzyman's reply I could successfully resolve the issue. The issue seems to be it is not able resolve the location of the urllib.py. We need to set it.

您可以检查以下的的问题和答案链接

这篇关于上的urllib导入错误 - 在C#应用程序中嵌入IronPython的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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