Visual Studio对Python版本感到困惑

时间:2020-04-19 19:27:04

标签: python visual-studio numpy environment-variables

我正在从C#文件中调用一个简单的Python脚本,

        //ADD results and errors  e.g., code Run GP tool.sln
        #region Running Python scripts Commented
        ProcessStartInfo psi = new ProcessStartInfo();
        //Script variables, paths etc.
        psi.FileName = @"C:\Users\oguz\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone1\python.exe";
        var py_script = @"C:\Users\oguz\source\repos\ProAppModuleDeneme5\ShapefileProcess\ShapefileProcess.py"; //change the path later
        var filename_old = shp_path;
        var filename_new = shp_path.Remove(21) + "F" + RenameTextBox.Text;
        psi.Arguments = $"{py_script} {filename_old} {filename_new}";
        MessageBox.Show(psi.Arguments);
        //Process configuration
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        //Execute and get output
        var errors = "";
        var results = "";

        using (var process = Process.Start(psi))
        {
            errors = process.StandardError.ReadToEnd();
            results = process.StandardOutput.ReadToEnd();
            MessageBox.Show(errors);
            MessageBox.Show(results);
        }

        Console.WriteLine(errors);
        Console.WriteLine(results);

此C#代码应将Python代码称为

import arcpy
import sys
import os



filename_old = sys.argv[1]
filename_new = sys.argv[2]

folder_path = "C:\\Projects\\Test"

arcpy.env.workspace = folder_path

arcpy.Rename_management(str(filename_old), str(filename_new))

我收到此消息

enter image description here

看起来Visual Studio调用了正确的Python,然后在需要库时尝试从其他Python环境中获取它。

我尝试了很多事情,但是结果是一样的。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

我删除了其他Python版本并保留了我需要的版本,然后它运行良好。我认为这些Python版本是Visual Studio附带的。那是我能够解决的唯一方法。希望有人会给出更好的答案。