使用C#导入运行Python Skript WITH导入

时间:2019-05-15 13:34:23

标签: c# python

在我的python中,我有一些像这样的导入:

import sys
import time
import datetime
import json
from modules.user import User
from modules.connector import Connector
from modules.request import Request
from modules.converter import Converter

我使用MSDM示例运行它 MSDN

结果如下:

string myPythonApp = @"c:\sap_toolkit_1\sap_bo_status.py";

// parameter
string pythonPath = @"C:\Program Files (x86)\Python\Python37-32\python.exe";
string output_folder = @"c:\temp";
string username = "myUser";
string password = "myPWD";
string environment = "GER_PROD";
string folder_id = "12345";
string technology_plant = "Fra";

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(pythonPath);

// make sure we can read the output from stdout 
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;

// start python app with arguments  
myProcessStartInfo.Arguments = myPythonApp + " "
                               + username + " "
                               + password + " "
                               + environment + " "
                               + folder_id + " "
                               + output_folder + " "
                               + technology_plant;

Process myProcess = new Process();
// assign start information to the process 
myProcess.StartInfo = myProcessStartInfo;

Console.WriteLine("Calling Python script with arguments {0}, {1}, {2}, {3}, {4}, {5}",
                  username,
                  password,
                  environment,
                  folder_id,
                  output_folder,
                  technology_plant);
// start the process 
myProcess.StartInfo.WorkingDirectory = myPythonApp;
myProcess.Start();

// Read the standard output of the app  
// in order to avoid deadlock we will read output first 
// and then wait for process terminate: 
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();

/*if you need to read multiple lines, you might use: 
    string myString = myStreamReader.ReadToEnd() */

// wait exit signal from the app we called and then close it. 
myProcess.WaitForExit();
myProcess.Close();

// write the output we got from python app 
Console.WriteLine("Value received from script: " + myString);

Console.ReadLine();

问题是C#似乎将在他的文件夹中运行该应用程序。然后缺少了python脚本的所有依赖项。

今天有没有解决办法?

谢谢


我认为这与最喜欢的问题不同。是的,想法是一样的,但是如果您搜索python和C#,将找不到它。随便我要发布包含示例代码的解决方案。

class Program
{
    static void Main(string[] args)
    {
        string myPythonApp = "main.py";
        string pythonPath = @"C:\Program Files (x86)\Python\Python37-32\python.exe";

        ProcessStartInfo pySkript = new ProcessStartInfo();
        pySkript.WorkingDirectory = @"D:\GitRepos\CallPythonByCSharp\FibonacciCalculator\";
        pySkript.FileName = pythonPath;
        pySkript.Arguments = string.Format("{0} {1}",myPythonApp, 1000);
        pySkript.UseShellExecute = false;
        pySkript.RedirectStandardOutput = true;

        System.Console.WriteLine("Start python programm!");

        using (Process process = Process.Start(pySkript))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                System.Console.Write(result);
            }
        }
        System.Console.ReadKey();
    }
}

python代码如下:

main.py

import modules
import sys


def start(n):
    myFibNo = modules.FibonacciNumbers(n)
    result = myFibNo.calc()
    print(result)
    print("Finished")


if __name__ == "__main__":
    start(int(sys.argv[1]))

子文件夹模块 init .py

from .fibonacci import FibonacciNumbers

和斐波那契计算器

class FibonacciNumbers:
    """Calulates the fibonacci sequence
    F(n) = F(n-1) + F(n-2) for n>1

    Args:
        arg n (int) the last possible number in fibonacci series
    """

    def __init__(self, n):
        self.n = n

    def calc(self):
        """Calculates the fibonacci series up to a specific number

        Returns:
            List of fibonacci numbers
        """
        result = []
        a, b = 0, 1
        while a < self.n:
            result.append(a)
            a, b = b, a+b
        return result

如果您要克隆存储库:

https://github.com/0x53olution/CallPythonByCSharp

0 个答案:

没有答案