简体   繁体   中英

Embedding IronPython in a C# application - import error on urllib

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

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'

(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

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

Any ideas?
I'd like to manage the imports in the python code and not in the C# code.
(So I'd like to avoid stuff like this: engine.ImportModule("urllib"); )

Edit: 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.

I believe that 'Lib' being on sys.path from the interactive console is actually done inside ipy.exe - and when embedding you will have to add the path manually. Either the engine or the runtime has a 'SetSourcePaths' (or similar) method that will allow you to do this.

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.

You can check the following link for the question and answer.

The version of CPython you're importing from must match your IronPython version. Use CPython v2.5 for IronPython 2.0, or v2.6 for IronPython 2.6.

Try this:

import sys
sys.path.append(r'\c:\python26\lib')  # adjust to whatever version of CPython you have installed.
import urllib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM