Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass arguments to a Python script with IronPython

I have the following C# code where I call a python script from C#:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

namespace RunPython
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
            ScriptRuntime runtime = new ScriptRuntime(setup);
            ScriptEngine engine = Python.GetEngine(runtime);
            ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);
        }
    }
}

I'm having trouble understanding each line of the code because my experience with C# is limited. How would I alter this code in order to pass a command line argument to my python script when I run it?

like image 899
GenericAlias Avatar asked May 21 '15 21:05

GenericAlias


People also ask

How do you pass arguments to a python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys.argv) will store all the information in the command line entry and can be accessed inside the Python script. Python’s getopt module can also be used to parse named arguments. Let’s go through some examples.

How do I pass a variable to a python script?

One approach is to parameterize some of the variables originally hard coded in the script, and pass them as arguments to your script. If you have only 1 or 2 arguments, you may find sys.argv is good enough since you can easily access the arguments by the index from argv list.

What is argument parsing in Python?

Argument Parsing in Python Python Server Side Programming Programming Every programming language has a feature to create scripts and run them from the terminal or being called by other programs. When running such scripts we often need to pass on arguments needed by the script for various functions to be executed inside the script.

How to print the fourth argument of a python script?

print('Fourth argument:', str(sys.argv[3])) Then execute the above script with command line parameters. python script.py first 2 third 4.5 You will see the results like below. The first argument is always the script itself.


1 Answers

Thank you all for pointing me in the correct direction. For some reason engine.sys seems to no longer work for more recent versions of IronPython so instead GetSysModule must be used. Here is the revised version of my code that allows me to alter argv:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

namespace RunPython
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
            ScriptRuntime runtime = new ScriptRuntime(setup);
            ScriptEngine engine = Python.GetEngine(runtime);
            ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
            ScriptScope scope = engine.CreateScope();
            List<String> argv = new List<String>();
            //Do some stuff and fill argv
            argv.Add("foo");
            argv.Add("bar");
            engine.GetSysModule().SetVariable("argv", argv);
            source.Execute(scope);
        }
    }
}
like image 115
GenericAlias Avatar answered Sep 21 '22 14:09

GenericAlias