views:

759

answers:

3

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.

A: 

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
fatcat1111
I was under the assumption that it would try to import from the IronPython lib folder. Anyway, the versions match (2.6), I added the code and it gives me a syntax exception. (when running it through the C# code in visual studio)When I run it directly in the command prompt the script executes normally. (both in IronPython and CPython)
Jeroen Pelgrims
Interesting. From your script (not interactively), what do you get when you evaluate os.environ.get('PYTHONPATH')?
fatcat1111
`import os` throws the same exception as urllib so I can't answer that question :). Interactively (Both in IronPython and CPython) it returns None Edit: I think fuzzyman might be on the right track, when printing `sys.path` from the script I only get `['.', 'C:\\DATA\\My Dropbox\\Projects\\C#\\IronPython reference\\DynamicTest\\DynamicTest\\bin\\Debug\\Lib']` Now I just have to find the right function to load the correct lib path.
Jeroen Pelgrims
+2  A: 

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.

fuzzyman
This seems to be the problem. sys.path only contains `['.', 'C:\\DATA\\My Dropbox\\Projects\\C#\\IronPython reference\\DynamicTest\\DynamicTest\\bin\\Debug\\Lib']` (the location of my project). I just need to find the correct function to add the correct library path. But that will be for tomorrow. This will also mean that the users of this program will need to install IronPython? I was hoping that when the C# application is compiled and the IronPython DLL files are included the program would be able to run. (unless I add the IronPython libraries as well?)
Jeroen Pelgrims
I copied the contents of the IronPython Lib folder to the location in sys.path and now I'm getting a syntax exception (invalid syntax) on the line `source.Execute(scope);` :/
Jeroen Pelgrims
In order to use the standard library modules you will need the standard library (the .py files) available. That doesn't mean that your users will need IronPython installed but you will need to ship the parts of the standard library (as a minimum) that your application uses.I don't know why you are getting a syntax error - other than possibly having invalid syntax... Is there any more information in the exception, does the same code run from ipy.exe?
fuzzyman
see: http://stackoverflow.com/questions/1755572/ironpython-scriptruntime-equivalent-to-cpython-pythonpath/1756818#1756818 for how to get/set engine search path.
Tom E
It runs normally from ipy.exe. The following is shown when I click 'Show Detail' when VS indicates the exception is thrown: http://i.imgur.com/a6mtb.png The content of StackTrace: http://ideone.com/oz2YyCvu The content of SourceCode is the content of os.py So from this I can only assume the problem must be connected with the os module. (urllib also imports os so it's only natural that the same problem would occur.)I have no idea what the problem is though.. A syntax error seems unlikely since it works with ipy.exeI would already like to thank you for your time up till now :)
Jeroen Pelgrims
A: 

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.

rAm