Example #1
0
        // Config the embedded CPython
        static void PythonInitialize()
        {
            //  Set the PATH env variable for OS pythonXX.dll searching
            //  or put the pythonXX.dll into the current working directory or a directory in PATH
            //  Attension, PythonHome cannot be used for dll search!
            //string python = @"C:\Python\WinPython-64bit-2.7.10.3\python-2.7.10.amd64";
            //string python = @"C:\Python\WinPython-64bit-3.5.2.2Qt5\python-3.5.2.amd64";
            //string python = "C:\\Anaconda3";
            string python = "C:\\Anaconda3.5";

            Environment.SetEnvironmentVariable("PATH", python, EnvironmentVariableTarget.Process);

            // it will be used to set the PythonPath with defaut values
            PythonEngine.PythonHome = python;
            //Console.WriteLine(PythonEngine.PythonPath);
            // Sometimes, we don't want to embed a completed CPython,
            // and we only want to distrute our app with miminum files
            // then, we can set the Python Module search Path by hand
            //PythonEngine.PythonPath = String.Join(";", new List<string>() {
            //    $"{python}\\DLLs",
            //    $"{python}\\Lib",
            //    $"{python}\\Lib\\site_packages",
            //});

            //PythonEngine.ProgramName = "RuePyNet";
            PythonEngine.Initialize();

            PythonEngine.BeginAllowThreads(); //need be removed into the future
            //redirect python print to .NET Console
            PySysIO.ToConsoleOut();
        }
Example #2
0
        public void ToConsoleOut()
        {
            // redirect to Console.Out, I usually put this at the entrance of a program.
            PySysIO.ToConsoleOut();

            //test
            using (Py.GIL())
            {
                PythonEngine.RunSimpleString("print('hello ConsoleOut!')");
            }
        }
Example #3
0
        public void ToNullTextWriter()
        {
            // redirect to a TextWriter
            PySysIO.ToTextWriter();

            //test
            using (Py.GIL())
            {
                PythonEngine.RunSimpleString("print('hello NullTextWriter!')");
            }
        }
Example #4
0
        public void ToTextWriter()
        {
            // redirect to a TextWriter
            PySysIO.ToTextWriter(new StringWriter());

            //test
            using (Py.GIL())
            {
                PythonEngine.RunSimpleString("print('hello TextWriter!')");
            }

            var writer  = PySysIO.TextWriter as StringWriter;
            var content = writer.GetStringBuilder().ToString();

            Console.WriteLine(content);
        }