Esempio n. 1
0
        static void Main(string[] args)
        {
            ScriptEngine pythonEngine =
                IronPython.Hosting.Python.CreateEngine();
            ICollection <string> searchPaths = pythonEngine.GetSearchPaths();

            // Now modify the search paths to include the directory
            // where the standard library has been installed
            searchPaths.Add(@"C:\Program Files\IronPython 2.7\Lib");
            pythonEngine.SetSearchPaths(searchPaths);

            // Execute the script
            // We execute this script from Visual Studio
            // so the program will executed from bin\Debug or bin\Release
            ScriptSource pythonScript =
                pythonEngine.CreateScriptSourceFromFile("example.py");

            pythonScript.Execute();

            var     scope = Python.ImportModule(pythonEngine, "HelloWorldModule");
            dynamic printHelloWorldFunction = scope.GetVariable("PrintHelloWorld");

            printHelloWorldFunction();

            dynamic printMessageFunction = scope.GetVariable("PrintMessage");

            printMessageFunction("Goodbye!");

            dynamic addFunction = scope.GetVariable("Add");

            System.Console.Out.WriteLine("The sum of 1 and 2 is " + addFunction(1, 2));
        }
Esempio n. 2
0
        private void Try_Runing_Click(object sender, EventArgs e)
        {
            //var engine = IronPython.Hosting.Python.CreateEngine();
            //ScriptScope scope = Python.ImportModule(engine, "math");
            //var options = new Dictionary<string, object>();
            //options["Frames"] = true;
            //options["FullFrames"] = true;
            //options["math"] = true;
            //ScriptRuntime pyRumTime = Python.CreateRuntime();
            //dynamic obj = pyRumTime.UseFile(@"D:\github\software\PyCommon\PyCommon.py");
            var engine = IronPython.Hosting.Python.CreateEngine();
            var scope  = Python.ImportModule(engine, "math");
            var source = engine.CreateScriptSourceFromFile(@"D:\github\software\PyCommon\PyCommon.py");

            source.Execute(scope);
            var say_hello = scope.GetVariable <Func <object, object> >("select");
            var aaa       = say_hello("C:\\Users\\tian\\Desktop\\人大\\岑巩县");

            var           py          = Python.CreateEngine();
            List <string> searchPaths = new List <string>();

            searchPaths.Add(@"C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib)");
            searchPaths.Add(@"C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib\site-packages)");
            py.SetSearchPaths(searchPaths);
            // object aaa = obj.select("C:\\Users\\tian\\Desktop\\人大\\岑巩县");
            object asd = aaa;
        }
Esempio n. 3
0
 private void TryLoadDebugger()
 {
     try {
         Python.ImportModule(scope, "pydevd");
         ipy.Execute("pydevd.settrace(stdoutToServer=True, stderrToServer=True)", scope);
     } catch (Exception e) {
         debug = false;
         Message(e.ToString());
     }
 }
Esempio n. 4
0
        private ScriptScope ExecScriptModule(string moduleName, string cmd)
        {
            var scope = Python.ImportModule(_scriptEngine, moduleName);

            try {
                _scriptEngine.Execute(cmd.Replace("\\", "/"), scope);
            }
            catch (Exception err)
            {
                string msg = _scriptEngine.GetService <ExceptionOperations>().FormatException(err);
                FtpToolMsgBox(msg);
            }
            return(scope);
        }
Esempio n. 5
0
    static void SetPyEnv(ScriptRuntime runtime, string pyscript, string[] args)
    {
        ScriptScope scope = Python.ImportModule(runtime, "sys");

        scope.SetVariable("version", "ironpython");

        IronPython.Runtime.List lst =
            (IronPython.Runtime.List)scope.GetVariable("argv");

        lst.Clear();
        lst.append(pyscript);
        foreach (string arg in args)
        {
            lst.append(arg);
        }
    }
Esempio n. 6
0
        //
        // 指定したフォルダ下にあるスクリプトファイルの一覧を取得してスクリプト選択用のコンボボックスに設定
        //
        private void GetScriptFiles(string path)
        {
            //指定のモジュールを読み込んで、モジュール内の関数を実行
            var scope = Python.ImportModule(_scriptEngine, "ftpToolUtils");

            //scope.SetVariable("__filePath", path);  //変数を設定してデータを渡す方法

            string cmd = "getScriptFiles('" + path + "')"; //関数の引数で渡す

            // ※パスの引き渡しがうまくいかないので、前もってパスを変換する必要がある
            //    例えば、"\\bin などとあると、どうも\b が最初に解釈されてしまい
            //    間違ったパスが指定された、というようなエラーを起こす。
            _scriptEngine.Execute(cmd.Replace("\\", "/"), scope);

            var sec = scope.GetVariable <IList <string> >("result");

            foreach (string fn in sec)
            {
                _scriptFiles.Add(new ScriptFile {
                    Id = 0, Name = System.IO.Path.GetFileNameWithoutExtension(fn), Description = "", Path = fn
                });
            }
        }
Esempio n. 7
0
 internal static dynamic Import(this ScriptRuntime runtime, string moduleName)
 {
     return(Python.ImportModule(Python.GetEngine(runtime), moduleName));
 }