【问题标题】:Python execute external Python Script in Console applicationPython 在控制台应用程序中执行外部 Python 脚本
【发布时间】:2017-02-21 00:51:15
【问题描述】:

这是我找到的控制台应用程序的代码示例here

static void Main(string[] args)
    {

        int a = 1;
        int b = 2;

        Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine(); // allow us to run ironpython programs
        Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope(); // you need this to get the variables
        py.Execute("x = "+a+"+"+b,s); // this is your python program
        Console.WriteLine(s.GetVariable("x")); // get the variable from the python program
        Console.ReadLine(); // wait for the user to press a button
    }

我正在使用 IronPython 尝试执行一个 Python 脚本,该脚本存储在一个名为 GuessingGameOne.pyinside 控制台应用程序窗口的外部文件中。

    import random   

def main():
    randomnumber = random.randint(1, 100)
    matched = False
    count = 0
    while (matched == False):
        inputnumber = input('Choose a number: ')        
        try:
            count = count+1
            inputnumber = int(inputnumber)
            if inputnumber > 0 and inputnumber < 101:
                comparenumber = inputnumber - randomnumber
                if comparenumber == 0:                
                    print("You guessed it. It took you " + str(count) + " guesses" )
                    matched = True
                elif comparenumber < 0:
                    print("Guess is too low")
                else:                
                    print("Guess is too high")
            else:
                print("Your guess must be between 1 and 100")

        except ValueError:
            print("Invalid Entry: Your guess must be a number")

if __name__ == "__main__":
   main()

如何更改 Main(string[] args) 以调用脚本 GuessingGameOne.py(如上所示)并在控制台窗口中运行它?

【问题讨论】:

  • 在向 SO 发布问题之前,您应该真正尝试了解这段代码在做什么。

标签: c# python console-application


【解决方案1】:

我没有测试过这段代码,但在这里找到了类似的答案:

https://stackoverflow.com/a/30385400/5258056

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

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

            // You don't need this section, but added in case
            // you decide to use it later.
            List<String> argv = new List<String>();
            //Do some stuff and fill argv
            argv.Add("foo");
            argv.Add("bar");
            engine.GetSysModule().SetVariable("argv", argv);

            // You will need this though....
            source.Execute(scope);
        }

您没有做的是指定要执行的文件位置。

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2021-07-17
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多