我正在尝试从我的c#文件调用Unity3d中的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#文件相同的目录、根目录和插件程序中。但我还是发现了这个错误。该怎么办呢?
发布于 2019-10-30 15:18:19
Pythonnet的Py.Import()的行为与Pythonnet的“导入”语句相同。要成功导入模块,模块必须位于Python解释器搜索路径的位置之一。解释器搜索路径是特定于平台的,可以修改.有关详细信息,请参阅Python文档:修改Python的搜索路径。
在Windows下,我通常使用以下两种选项之一:
与Python文档中列出的其他选项相比,使用这些选项的优点是不修改Python安装。第二个选项也适用于忽略环境变量的可嵌入Python。仅针对特定进程修改PYTHONPATH的优点是您不影响系统中的其他进程。但是,如果您只需要快速测试一些东西,就可以直接修改系统的PYTHONPATH环境变量。
https://stackoverflow.com/questions/58456303
复制相似问题