小编sde*_*per的帖子

Python for .NET:ImportError:没有名为warnings的模块

我试图在.NET 4.0中嵌入python 2.6.

按照"Python for .NET"中的最小文档,我编写了一个相当简单的代码,如下所示:

 const string pythonModulePath = @"C:\Projects\PythonImport\PythonImport\test.py";
 Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(python
 ModulePath));

 PythonEngine.Initialize();

 var oldWorkingDirectory = Directory.GetCurrentDirectory();
 var currWorkingDirectory = Path.GetDirectoryName(pythonModulePath);

 Directory.SetCurrentDirectory(currWorkingDirectory);

 var pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(python
 ModulePath));
 if (pyPlugin == null)
 {
      throw new PythonException();
 }

 Directory.SetCurrentDirectory(oldWorkingDirectory);
Run Code Online (Sandbox Code Playgroud)

即使test.py为空,我也会收到导入错误,说"没有名为warnings的模块".

'import site' failed; use -v for traceback

 Unhandled Exception: Python.Runtime.PythonException: ImportError : No module nam
 ed warnings
Run Code Online (Sandbox Code Playgroud)

当你使用"python -v"(详细模式)运行test.py时,原因就变得很明显了.请注意,python调用#cleanup [2]警告.

为什么Python .NET无法解决警告?该模块位于Lib目录中.

有任何想法吗?

.net python python.net

8
推荐指数
1
解决办法
3057
查看次数

标签 统计

.net ×1

python ×1

python.net ×1