I’m using the following C# code to call a Python script using IronPython:
ScriptEngine scriptEngine;
var opts = new Dictionary<string, object>();
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
unpacks the list of arguments in
sys.argvinto the four variables on the left. Since there are only three arguments insys.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 insys.argv).Try a different way of passing in command-line arguments (from this answer):
Alternatively, if that doesn’t work, then either remove the
arg0variable from the assignment in the Python file, or add the script’s path explicitly as the first argument in C#.