gpt4 book ai didi

c# - 设置 ironpython : Module not found

转载 作者:行者123 更新时间:2023-12-04 17:40:16 25 4
gpt4 key购买 nike

我第一次使用 Ironpython 将 Python 脚本导入 C#。我收到错误“No module named numpy”,但我不知道为什么。我读到我必须将我的模块路径添加到我的 python 脚本中。这是我的 python 脚本:

import numpy as np
import sys
sys.path.append(r"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib")
sys.path.append(r"C:\Users\abc\PycharmProjects\untitled3\venv\Lib")
sum = np.sum([0.5, 1.5])
print(sum)

第二个路径也是在 Pycharm 中用作 python.exe 的项目解释器的路径。

我的 C# 代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonScriptExecution2
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine pythonEngine =
IronPython.Hosting.Python.CreateEngine();
// We execute this script from Visual Studio
// so the program will be executed from bin\Debug or bin\Release
Microsoft.Scripting.Hosting.ScriptSource pythonScript =
pythonEngine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
pythonScript.Execute();
}
}
}

在Pycharm中运行Python脚本没问题,导入到C#中会出现上述错误。谁能帮我设置正确的路径?

编辑:如果它不起作用,有没有人知道用 C# 运行 python 脚本的任何其他方法?

最佳答案

How to add modules to Iron Python?相关

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonScriptExecution2
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine eEngine =
IronPython.Hosting.Python.CreateEngine();
// We execute this script from Visual Studio
// so the program will be executed from bin\Debug or bin\Release
Microsoft.Scripting.Hosting.ScriptSource pythonScript =

ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib");
searchPaths.Add(@"C:\Users\abc\PycharmProjects\untitled3\venv\Lib");
engine.SetSearchPaths(searchPaths);

engine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
pythonScript.Execute();
}
}
}

关于c# - 设置 ironpython : Module not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54863680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com