简体   繁体   English

Unity3D: (Python.net) PythonException: ModuleNotFoundError

[英]Unity3D: (Python.net) PythonException: ModuleNotFoundError

I am trying to call a python class from my c# file in Unity3d.我正在尝试从 Z1DEF5B505372521FD984Z197E9124166 中的 c# 文件中调用 python class Numpy and os modules work fine. 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 is my custom class clrTest.py clrTest 是我的自定义 class 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

I get this following error我收到以下错误

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)

I tried placing my python file in same directory as the c# file, in the root directory and in the Plugins foler.我尝试将我的 python 文件放在与 c# 文件相同的目录中,在根目录和插件文件夹中。 Still I get this error.我仍然收到此错误。 What to do?该怎么办?

Pythonnet's Py.Import() behaves the same as Python "import" statement. Pythonnet 的 Py.Import() 行为与 Python "import" 语句相同。 For a module to be successfully imported, the module has to be in one of the locations of Python interpreter's search path.要成功导入模块,该模块必须位于 Python 解释器搜索路径的位置之一。 The interpreter search path is platform-specific and can be modified.解释器搜索路径是特定于平台的,可以修改。 Refer to Python documentation for details: Modifying Python's Search Path .有关详细信息,请参阅 Python 文档: 修改 Python 的搜索路径

Under Windows, I usually resort to one of the two options:在 Windows 下,我通常采用以下两个选项之一:

  1. Modify PYTHONPATH environment variable for the managed process:修改托管进程的 PYTHONPATH 环境变量

     Environment.SetEnvironmentVariable("PYTHONPATH", @"D:\MyPythonModules\", EnvironmentVariableTarget.Process);
  2. Modify Python's sys.path by executing python code string with pythonnet:通过使用 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 } }

The advantage of using these options over others listed in Python documentation is that you don't modify your Python installation.与 Python 文档中列出的其他选项相比,使用这些选项的优势在于您无需修改 Python 安装。 The second option will also work withembeddable Python which ignores environment variables.第二个选项也适用于忽略环境变量的嵌入式 Python The advantage of modifying PYTHONPATH only for the specific process is that you don't influence other processes in the system.仅针对特定进程修改 PYTHONPATH 的优点是您不会影响系统中的其他进程。 However, if you need only to test something quickly, you can directly modify the system's PYTHONPATH environment variable.但是,如果你只需要快速测试一些东西,你可以直接修改系统的 PYTHONPATH 环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM