C# Unity3D:(Python.net)PythonException:ModuleNotFoundError
我试图从Unity3d中的c#文件中调用python类。Numpy和os模块工作正常C# Unity3D:(Python.net)PythonException:ModuleNotFoundError,c#,python,unity3d,import,python.net,C#,Python,Unity3d,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
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
}
}
我得到以下错误
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)
PythonException:ModuleNotFoundError:没有名为'clrTest'的模块
Python.Runtime.Runtime.CheckExceptionOccurred()(位于:0)
Python.Runtime.PythonEngine.ImportModule(System.String名称)(位于:0)
Python.Runtime.Py.Import(System.String名称)(位于:0)
我尝试将python文件放在与c#文件相同的目录中,根目录和插件文件夹中。但我还是犯了这个错误。怎么办?Pythonnet的Py.Import()行为与Python的“Import”语句相同。要成功导入模块,该模块必须位于Python解释器搜索路径的某个位置。解释器搜索路径是特定于平台的,可以修改。有关详细信息,请参阅Python文档:
在Windows下,我通常采用以下两种选择之一:
Environment.SetEnvironmentVariable("PYTHONPATH", @"D:\MyPythonModules\", EnvironmentVariableTarget.Process);
using (Py.GIL())
{
int returnValue = PythonEngine.RunSimpleString("import sys;sys.path.insert(1, 'D:/MyPythonModules/');");
if (returnValue != 0)
{
//throw exception or other failure handling
}
}
Environment.SetEnvironmentVariable("PYTHONPATH", @"D:\MyPythonModules\", EnvironmentVariableTarget.Process);
using (Py.GIL())
{
int returnValue = PythonEngine.RunSimpleString("import sys;sys.path.insert(1, 'D:/MyPythonModules/');");
if (returnValue != 0)
{
//throw exception or other failure handling
}
}
仅为特定进程修改PYTHONPATH的优点是不会影响系统中的其他进程。但是,如果只需要快速测试某些内容,可以直接修改系统的PYTHONPATH环境变量。是否尝试重建,重新启动项目?是否文件名实际上是
clrTest.py
而不是py.Import
的clrTest
?是否文件名实际上是clrTest.py
而不是clrTest
的py.Import
?