2016-12-09 2 views
6

.NETコアアプリケーションでPythonを使用するには?私はHackathonの目的のためにこれを必要とするので、ソリューションは「エレガント」である必要はありません。標準のASP.NETではライブラリIronPythonが存在し、.NETコアでは存在しないため、Pythonスクリプトを直接実行することは不可能だということを読んだことがあります。 Pythonスクリプトを使用する最も簡単な方法は何ですか? (それはハッカソンだからそれだけでスクリプトを実行するためにも、PHPサーバーやセレンなどを使用しても大丈夫です).NETコアアプリケーションでPythonを使用するには?

+4

をお試しください: IronPythonはまだ.netコアでは利用できません。まだ開発中で、.netコア1.1または1.2が付属しています。https://github.com/IronLanguages/ironpython3/issues/77を参照してください。 –

+0

これは同じです。私は生きているPythonスクリプトでは見たことがありませんが、私は自分のスクリプトを置くことができ、私のASPアプリケーションからJSONによってパラメータを投稿し、結果を得ることができるウェブサイトを見つける/作ることができると思います。可能です? –

+1

あなたはPython上の道を行くことができ、Python web-api(django、...)を作り、C#からweb-apiを呼び出します。しかし、私は思っていません、それは可能です(少なくとも今のところ)Pythonをネットコアと組み合わせることは可能です。ネットコアはまだ若々しい若いです。あるいは、あなたは、Pythonスクリプトを書いて、 'Process'でそれらを呼び出し、python.exeをscript-pathで呼び出すことができます。私は他の方法を知らない... –

答えて

0

は、.NETコアのためだけFYI IronPythonの程度この

public class RunCmd 
{ 
    public string Run(string cmd, string args) 
    { 
     ProcessStartInfo start = new ProcessStartInfo(); 
     start.FileName = "python"; 
     start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args); 
     start.UseShellExecute = false;// Do not use OS shell 
     start.CreateNoWindow = true; // We don't need new window 
     start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back 
     start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions) 
     using (Process process = Process.Start(start)) 
     { 
      using (StreamReader reader = process.StandardOutput) 
      { 
       string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script 
       string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test") 
       return result; 
      } 
     } 
    } 
} 

その後

var res = new RunCmd().Run("your_python_file.py","params"); 
Console.WriteLine(res); 
関連する問題