c# - Unity3D : (Python.net) PythonException: ModuleNotFoundError

标签 c# python unity-game-engine import python.net

我正在尝试从 Unity3d 中的 c# 文件调用 python 类。 Numpy 和 os 模块工作正常。

 void Start()
    {
        startTime = Time.time;

        using (Py.GIL())
        {
            dynamic np = Py.Import("numpy");
            UnityEngine.Debug.Log(np.cos(np.pi * 2));

            dynamic sin = np.sin;
            UnityEngine.Debug.Log(sin(5));

            double c = np.cos(5) + sin(5);
            UnityEngine.Debug.Log(c);

            dynamic a = np.array(new List<float> { 1, 2, 3 });
            UnityEngine.Debug.Log(a.dtype);

            dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
            UnityEngine.Debug.Log(b.dtype);

            UnityEngine.Debug.Log(a * b);
            dynamic os = Py.Import("os");
            UnityEngine.Debug.Log(os.getcwd());

            dynamic test = Py.Import("clrTest"); // this throws me an error
        }
    }

clrTest 是我的自定义类clrTest.py

class clsMyTest:
    """clsTest.clsMyTest class"""

    @staticmethod
    def Test03():
        return 42

    def Test04():
        return 42

def Test01():
    return 42

@staticmethod
def Test02():
    return 42

我收到以下错误

PythonException: ModuleNotFoundError : No module named 'clrTest'
Python.Runtime.Runtime.CheckExceptionOccurred () (at <38fa310f96774b388b3fb5f7d3ed5afc>:0)
Python.Runtime.PythonEngine.ImportModule (System.String name) (at <38fa310f96774b388b3fb5f7d3ed5afc>:0)
Python.Runtime.Py.Import (System.String name) (at <38fa310f96774b388b3fb5f7d3ed5afc>:0)

我尝试将 python 文件放在与 c# 文件相同的目录中、根目录和插件文件夹中。我仍然收到这个错误。该怎么办?

最佳答案

Pythonnet 的 Py.Import() 行为与 Python“导入”语句相同。为了成功导入模块,该模块必须位于 Python 解释器搜索路径的位置之一。解释器搜索路径是特定于平台的并且可以修改。详细信息请参阅Python文档:Modifying Python’s Search Path .

在 Windows 下,我通常采用以下两个选项之一:

  1. 修改托管进程的 PYTHONPATH 环境变量:

    Environment.SetEnvironmentVariable("PYTHONPATH", @"D:\MyPythonModules\", EnvironmentVariableTarget.Process);  
    
  2. 通过使用 pythonnet 执行 python 代码字符串来修改 Python 的 sys.path:

        using (Py.GIL())
        {
            int returnValue = PythonEngine.RunSimpleString("import sys;sys.path.insert(1, 'D:/MyPythonModules/');");
            if (returnValue != 0)
            {
                 //throw exception or other failure handling
            }
        }
    

与 Python 文档中列出的其他选项相比,使用这些选项的优点是您无需修改​​ Python 安装。第二个选项也适用于 embeddable Python它忽略环境变量。 仅针对特定进程修改 PYTHONPATH 的优点是不会影响系统中的其他进程。但是,如果您只需要快速测试某些内容,可以直接修改系统的 PYTHONPATH 环境变量。

关于c# - Unity3D : (Python.net) PythonException: ModuleNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58456303/

相关文章:

python - 在列表中搜索一对整数的算法

python - 带有管道和 GridSearchCV 的 StandardScaler

c# - 404 错误消息 - 找不到 SignalR/Hub

c# - 我可以使用其他类的函数作为委托(delegate)吗?

python - 在 Python 中非阻塞地收集子进程输出

php - 有没有办法将 UMA 头像配方**包括其转义字符**保存到我的数据库?

c# - 在运行时获取 Unity 编辑器专用变量(启动屏幕背景的颜色)

c# - 在鼠标坐标处将预制件实例化到 Canvas

c# - 什么是 Plugin CRM 2011 中的 Context、serviceFactory 和 Service

c# - 如何在 XDocument 中添加命名空间和声明