I'm using the following C# code to call a Python script using IronPython:
ScriptEngine scriptEngine;
var opts = new Dictionary<string, objec开发者_如何学JAVAt>();
opts["Arguments"] = new[] {
Server.MapPath(@"~\Processing\input.7z"), // Input
Server.MapPath(@"~\Processing\key.pem"), // Key
Server.MapPath(@"~\Processing\") }; // Output
scriptEngine = Python.CreateEngine(opts);
var sp = scriptEngine.GetSearchPaths();
sp.Add(Server.MapPath(@"~\python\lib"));
scriptEngine.SetSearchPaths(sp);
var scope = scriptEngine.Runtime.ExecuteFile(Server.MapPath(@"~\python\test.py"));
The script takes the following arguments:
arg0,input,key,output = sys.argv
I'm getting the error "Need more than 3 values to unpack". What am I doing wrong?
The line
arg0,input,key,output = sys.argv
unpacks the list of arguments in sys.argv
into the four variables on the left. Since there are only three arguments in sys.argv
, this fails with the error message you posted (apparently you need to manually pass in the script path for it to appear as the first element in sys.argv
).
Try a different way of passing in command-line arguments (from this answer):
scriptEngine.Sys.argv = List.Make(new[] { 'input.7z', ... });
Alternatively, if that doesn't work, then either remove the arg0
variable from the assignment in the Python file, or add the script's path explicitly as the first argument in C#.
Well the literal meaning of that error is that sys.argv
contains only 3 values, but you've got 4 variables there. (The error message that you'd get if you were trying to unpack 4 values into 3 variables would be "too many values to unpack".)
Why there's no arg0
in sys.argv
, I don't know -- I can only assume that it has something to do with the way .NET handles arguments in cases like these. Remove the arg0
and see what happens.
精彩评论