19

I'm a complete noob when it comes to IronPython. I need to call a py script from an ASP.NET website, and have the following code:

var ipy = IronPython.Hosting.Python.CreateRuntime();
dynamic test = ipy.UseFile(Server.MapPath(@"~\python\test.py"));
test.DoWork();

The version of IronPython I'm using is 2.7.

The 3rd party python file I need to call has the following import directives:

import sys
from array import *
from subprocess import *

I'm receiving the error "No module named subprocess". I've copied the subprocess.py file from the IronPython installation Lib directory, but I assume I need to link to it in the C# code?

Thanks.

EDIT:

I found the solution:

ScriptEngine scriptEngine = Python.CreateEngine();

var sp = scriptEngine.GetSearchPaths();
sp.Add(Server.MapPath(@"~\python\lib"));
scriptEngine.SetSearchPaths(sp);

var scope = scriptEngine.Runtime.ExecuteFile(Server.MapPath(@"~\python\test.py"));

If anyone has any comments/improvements please feel free to elaborate seeing as I'm in new ground with all this Python/IronPython malarky.

2
  • 1
    Just a note: you could also do this from within the Python script if you wanted to, by modifying sys.path. Also, you may want to post your solution as an answer and accept it, instead of keeping it in the question body itself. Commented Jul 2, 2011 at 15:33
  • Thanks Cameron. I tried to post it as an answer but as I'm a new user I was unable to for 8 hours. I'm having a new issue now attempting to pass in arguments... Commented Jul 2, 2011 at 16:00

1 Answer 1

2

An alternate way of doing this:

dynamic sys = scriptEngine.ImportModule("sys");            
sys.path.append(somePath);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.